How to get file creation & modification date/times in Python?

Getting some sort of modification date in a cross-platform way is easy – just call os.path.getmtime(path) and you’ll get the Unix timestamp of when the file at path was last modified.

Getting file creation dates, on the other hand, is fiddly and platform-dependent, differing even between the three big OSes:

Putting this all together, cross-platform code should look something like this…

import os
import platform

def creation_date(path_to_file):
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See for explanation.
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
        stat = os.stat(path_to_file)
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime

Solution 2:

You have a couple of choices. For one, you can use the os.path.getmtime and os.path.getctime functions:

import os.path, time
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))

Your other option is to use os.stat:

import os, time
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print("last modified: %s" % time.ctime(mtime))

Note: ctime() does not refer to creation time on *nix systems, but rather the last time the inode data changed.

Solution 3:

The best function to use for this is os.path.getmtime(). Internally, this just uses os.stat(filename).st_mtime.

The datetime module is the best manipulating timestamps, so you can get the modification date as a datetime object like this:

import os
import datetime
def modification_date(filename):
    t = os.path.getmtime(filename)
    return datetime.datetime.fromtimestamp(t)

Usage example:

>>> d = modification_date('/var/log/syslog')
>>> print d
2009-10-06 10:50:01
>>> print repr(d)
datetime.datetime(2009, 10, 6, 10, 50, 1)


Source : How to get file creation & modification date/times in Python? – Stack Overflow

Python – Try and Except statements

The try and except statements in python helps us to mitigate exception errors with a meaning full way. To understand why we require try and except statements we should know how python handles execution and syntax errors.

There are two types of errors in Python

	1)Syntax errors
	2)Exception errors

Syntax errors occur when there is a wrong syntax in your code and where as exception errors occur when executing the code and found something wrong with file permissions, floating point error, division by zero error, IO errors, Name errors etc though the syntax of the code is right one.

Try and except statements are useful in countering these exception errors. When ever your code come across these exceptions, your try and exception statements help in changing the behavior of your program in a meaning full way.

Let us learn try and except statements with examples
Try and Except statement syntax:

	    do something here
	except exception-code:
	    do something if found exception-code error

Example 1: Open a file and secure the error with IOError exception if file do not exit’s

	    f = open('abci.txt', 'r')
	except IOError:
	    print "Can not open the file"

Example 2: Ok, that is fine how about if you try to give illegal mode to open a file like ‘d’ mode? We have to counter this with multiple exceptions which may occur

	    f = open('abc.txt', 'dsf')
	    print "Sucessfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	except ValueError:
	    print "Can not open the file because some another thing happen"

Example 3: Clubbing multiple exception to form compound statement

	    f = open('abc.txt', 'd')
	    print "Sucessfully opened the file"
	except IOError and ValueError:
	    print "Can not open the file because due to file open issues "

Example 4: Printing actual exception in output so that user will come to know what kind of error he is getting.

	    f = open('abc.txt', 'dsf')
	    print "Successfully opened the file"
	except IOError as error1:
	    print "Can not open the file because I got ", error1

Example 5: Printing actual trace back with exception in output so that user will come to know what kind of error he is getting by using raise statement.

	    f = open('abc.txt', 'r')
	    print "Successfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues with below trace back"

Example 6: How about executing remaining code if there is no exception occur? Try and except can be combined with else statement so that if exception did not occur else block code is executed

	    f = open('abc.txt', 'r')
	    print "Sucessfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	    print "I am in else loop"

Example 7: How about executing some code once this try-except loop is executed and want to execute this code irrespective of exception occur or not. To do this we have to use finally statement to execute this code.

	    f = open('abc.txt', 'r')
	    print "Successfully opened the file"
	except IOError:
	    print "Can not open the file because due to file open issues "
	    print "I am in else loop"
	    print "This is the code finally>executed for this try example"

We should know below key words to understand try and exception statements

	try: --Will try to execute code which may contain exception
	except: --Once this exception occur and matches the exception which we are looking, then the code in this exception block is executed.
	raise: --Nothing but actual trace back with complete error message
	finally: --The code which we want to execute finally once try and except/else blocks are executed. This is equal to END block in Perl and AWK.

	else: --If no exception occur execute the code in this block.

Applications for Python

Applications for Python

This one leads to pythons official site.
Yes, some of the best, if not at least what is recommended can be found there.
Its a great place for resources if you actually take the time to look into it.

Many people start off in python, go there just download the latest python version,
and then get teleported to other sites or resources on the web, sure there are great resources out there,
non the less this might be a mistake to beginners as they end up looking into to much info that are irrelevant
at a start point and land on stuff that aren’t really going to help them out.

One should always start there and check out watt they start by recommending.
After all thats maintained by the people behind the python language.

How To Package And Distribute Python Applications – Linux

How To Package And Distribute Python Applications

How To Package And Distribute Python Applications


All Python libraries (i.e. application packages) that you download using a package manager (e.g. pip) are distributed using a utility dedicated to do the job. These utilities create « Python distributions » which are basically versioned (and compressed) archives. All related elements to what’s being distributed, such as source files and resource files, are contained within it.

In this DigitalOcean article, we are going to talk about the necessary tools for distribution and go over the key steps to allow you to package your own useful libraries, modules, or applications — which should help you when deploying your project on a droplet or sharing on the internet.

Python Distributions and Packages

Even if you have worked only a little with Python, you will be familiar with the concept of using a package manager (e.g. pip, easy_install) to download modules and libraries (e.g. application development frameworks) which are then imported and used to create a new one.

These package management tools, operating locally, connect to a source (i.e. Python Package Index – PyPI) and perform a desired action (e.g. search and install) as they work these resources which are actually called Python distributions.

The way to distribute an application consists of wrapping its directory with some must-have files (along with a few recommended ones), specifying related elements (e.g. resources, dependencies etc.) and releasing it or using it elsewhere…that simple.

Note: You are highly encouraged to work with virtual environments to isolate Python downloads, modules, and applications you are working with.

Python Packages

In Python, a package [technically] is an importable directory (with containing source files (i.e. modules). This shall not be confused with operating-system packages, which are [technically] actual applications (i.e. a Debian package). However, it must be noted that Python distributions are indeed called packages as well.

Example package structure:


Python Applications

Although anything from a single file to one with hundreds scattered across various packages can be considered an application in Python, in most realistic scenarios, an application will consist of multiple modules and a certain amount of external imports (from libraries).

Example application structure:

  |__ tests
  |     |
  |     |--
  |     |-- ..
  |     |-- .
  | ..

Python Distribution Tools and Libraries

Given the popular nature of Python and the rich amount of third-party libraries / applications written for it, a simpler and unified way of distributing has always been a necessity. There have been several different tools and libraries used for creating Python distributions.

In order to deal with the tasks of distribution, Python distribution utilities toolset distutils was created.

Python Package Index (PyPI)

Python Package Index, or PyPI, is a central [online] repository for projects (Python distributions). Package managing tools such as pip use this repository in order to host, find and install them.

Getting Started

Let »s begin with creating a simple, general Python flask application [structure] which we then can use to package.

Creating the Application Structure

We aim to create an example that resembles most real-world projects. Therefore, it will be best to imagine a scenario with modularised components.

Example structure:

    |__ /app
         |-- /module_one
         |__ /templates
             |-- module_one
                 |-- hello.html
         |__ /static
         |__ ..
         |__ .

Create the folders:

mkdir ~/MyApplication
cd    ~/MyApplication
mkdir app
cd    app
mkdir templates
mkdir static
mkdir module_one
cd    module_one
cd    ../templates
mkdir module_one
cd    module_one
touch hello.html

Edit using nano:

nano ~/MyApplication/

Place the contents:

# Run a test server.
from app import app

Save and exit using CTRL+X and confirm with with Y.

Edit using nano:

nano ~/MyApplication/

Place the contents:

DEBUG = True



Save and exit using CTRL+X and confirm with with Y.

Edit app/ using nano:

nano ~/MyApplication/app/

Place the contents:

from flask import Flask, render_template

app = Flask(__name__)

from app.module_one.controllers import module_one


Save and exit using CTRL+X and confirm with with Y.

Edit app/module_one/ using nano:

nano app/module_one/

Place the contents:

from flask import Blueprint, request, render_template

module_one = Blueprint("auth", __name__, url_prefix="/auth")

def hello():
    return render_template("module_one/hello.html")

Save and exit using CTRL+X and confirm with with Y.

Place the contents:

Edit app/templates/module_one/hello.html using nano:

nano app/templates/module_one/hello.html

Place the contents:

    &lt!DOCTYPE html>
    &lthtml lang="en">
        &lttitle>{% block title %}My Site{% endblock %}
        {% block css %}
        {% endblock %}
        &ltmeta name="viewport" content="width=device-width, initial-scale=1.0">
    Hello, world!

Save and exit using CTRL+X and confirm with with Y.

Beginning with Application Distribution / Packaging

Having created an exemplary application structure of a web site that uses flask, we can continue with taking the first step into preparing the distribution.

Altering the Folder Structure

In order to package our application well, we need to make some additions to our folder structure.

    |__ /app
         |-- /module_one
         |__ /templates
             |-- module_one
                 |-- hello.html
         |__ /static
         |__ ..
         |__ .
    |--    # Distribution setup file
    |-- README.txt  # Read-me file
    |-- # Distribution manifest file
    |-- CHANGES.txt # Changes log

Alter the folder structure to create necessary files:

touch ~/MyApplication/
touch ~/MyApplication/
touch ~/MyApplication/
touch ~/MyApplication/
mv    ~/MyApplication/ ~/MyApplication/bin/run

Create the

nano ~/MyApplication/

Place the below self explanatory contents:

from distutils.core import setup

    # Application name:

    # Version number (initial):

    # Application author details:
    author="name surname",

    # Packages

    # Include additional files into the package

    # Details

    # license="LICENSE.txt",
    description="Useful towel-related stuff.",

    # long_description=open("README.txt").read(),

    # Dependent packages (distributions)

Save and exit using CTRL+X and confirm with with Y.

Create the

If you need to ship extra directories (e.g. static or templates), you need to explicitly state them in the manifest to be packaged. We will do this inside the

nano ~/MyApplication/

Place the below self explanatory contents:

recursive-include app/templates *
recursive-include app/static *

Save and exit using CTRL+X and confirm with with Y.

And that’s it! Your Python distribution package is ready to be installed and shipped.

Additional Files

Please remember that in order to have a complete distribution, your file/directory must contain (and linked):

  • README.txt
  • LICENSE.txt

Working With the Distribution Ready Application

As we have finalized creation of our application followed by making necessary amendments to the file structure to prepare it for a flawless distribution build, we can begin with going through the packaging operations.

How to Create The Distribution File

In order to generate a distribution file copy, run the following:

cd     ~/MyApplication
python sdist

This command will go through your setup, print out the operations being performed and generate a tar archive inside the newly created diet directory, similar to:

# root@hostname:~/MyApplication# ls dist
# MyApplication-0.1.0.tar.gz

Note: Since we did not populate all the sub-folders (i.e. static) and worked with additional files (e.g. README.txt), you might see some warnings during the creation process.

How to Install The Application

From now on, your application can be installed and used by others using the file created.

In order to install the application, run the following:

python install

If this installation is for development and the requirements are also to be installed, run the following:

python develop

How to Share Your Application

If you would like to share your code on the Python Packaging Index, you can do so by initiating the « register » procedure as per the following:

python register

You will need to complete the procedure by following the on-screen instructions.

If you have a registered login, in order to just upload, you can use the following:

python sdist upload

How to Create Packages of Your Application’s New Versions

  1. Edit the file with a text editor (e.g. nano) and set the new version number: version="0.1.1"
  2. Edit the CHANGES.txt to reflect the changes
  3. Make the necessary adjustments to the LICENSE.txt and README.txt
  4. Upload your code following the previous step.

Article source:

Python: Write Gui Apps

This is an overview of the best tools and the best resources for building desktop applications in Python.

First things first. You can build great desktop applications in Python, and some are widely used (like Dropbox). But you’ll have to find your own way much more than you would using Microsoft’s or Apple’s SDKs. The upside is that, with a bit of legwork to package it appropriately, it’s quite feasible to write a Python application that works on all the major platforms.

GUI toolkits

The first thing you’ll need to choose is a GUI toolkit.

Qt logo

  • For traditional desktop UIs, Qt is a clear winner. It’s powerful, looks native on all the major platforms, and has probably the biggest community. There are two different Python bindings: PyQt is older and more mature, but it’s only free if your application is open source (licensing), while PySide is newer and more permissively licensed (LGPL). I refer to the main Qt docs a lot – the C++ examples mostly translate to Python quite well – but both PyQt’s and PySide’s docs contain some useful information. Qt Designer is a drag and drop interface to design your UI; you can compile its .ui files to Python modules with the pyuic command line tool.

Qt Designer in action

For attractive, tablet-style interfaces, Kivy is the right choice. It’s a fairly young but promising system. If you want to bring your application to tablets and smartphones, then Kivy is the only option that I’m aware of. More info

  • When you want a basic GUI and don’t care about aesthetics, Tkinter is a simple option. It’s installed as part of Python. Python’s own tkinter documentation is rather minimal, but it links to a bunch of other resources. This site is my favourite – it hasn’t been updated in years, but then neither has Tkinter (except that in Python 3, you import tkinter rather than import Tkinter).
  • pygame is popular for building simple 2D games. There are also frameworks for 3D graphics (pyglet, Panda3d), but I don’t know much about them.
  • An increasingly popular option is to write your application as a local web server, and build the UI in HTML and Javascript. This lets you use Python’s large ecosystem of web frameworks and libraries, but it’s harder to integrate with desktop conventions for things like opening files and window management. CEF Python lets you make a window for your application, based on Google Chrome, but I haven’t tried that.

A couple of alternatives I wouldn’t recommend unless you have a reason to prefer them: GTK is popular on Linux, but it looks ugly on other platforms. The older pygtk bindings have excellent documentation; the newer PyGObject system, which supports recent versions of GTK and Python, doesn’t (though it’s getting better). wx seems to have a good community, but development is slow, and new projects that could have used it now mostly seem to pick Qt.

Packaging and Distribution

This is probably the roughest part of making an application in Python. You can easily distribute tools for developers as Python packages to be installed using pip, but end users don’t generally have Python and pip already set up. Python packages also can’t depend on something like Qt. There are a number of ways to package your application and its dependencies:

  • Pynsist, my own project, makes a Windows installer which installs a version of Python that you specify, and then installs your application. Unlike the other tools listed here, it doesn’t try to ‘freeze’ your application into an exe, but makes shortcuts which launch .py files. This avoids certain kinds of bugs.
  • cx_Freeze is a freeze tool: it makes an executable out of your application. It works on Windows, Mac and Linux, but only produces the executable for the platform you run it on (you can’t make a Windows exe on Linux, for example). It can make simple packages (.msi for Windows, .dmg for Mac, .rpm for Linux), or you can feed its output into NSIS or Inno Setup to have more control over building a Windows installer.
  • PyInstaller is similar to cx_Freeze. It doesn’t yet support Python 3 (update: it does now, since October 2015), but it does have the ability to produce a ‘single file’ executable.
  • py2app is a freeze tool specifically for building Mac .app bundles.
  • py2exe is a Windows-only freeze tool. Development stopped for a long time, but at the time of writing there is some recent activity on it.

Linux packaging

Although some of the freeze tools can build Linux binaries, the preferred way to distribute software is to make a package containing just your application, which has dependencies on Python and the libraries your application uses. So your package doesn’t contain everything it needs, but it tells the package manager what other pieces it needs installed.

Unfortunately, the procedures for preparing these are pretty complex, and Linux distributions still don’t have a common package format. The main ones are deb packages, used by Debian, Ubuntu and Mint, and rpm packages, used by Fedora and Red Hat. I don’t know of a good, simple guide to packaging Python applications for either – if you find one or write one, let me know.

You can get users to download and install your package, but if you want it to receive updates through the package manager, you’ll need to host it in a repository. Submitting your package to the distribution’s main repositories makes it easiest for users to install, but it has to meet the distro’s quality standards, and you generally can’t push new feature releases to people except when they upgrade the whole distribution. Some distributions offer hosting for personal repos: Ubuntu’s PPAs, or Fedora’s Fedorapeople repositories. You can also set up a repository on your own server.

If you don’t want to think about all that, just make a tarball of your application, and explain to Linux users next to the download what it requires.


  • Threading: If your application does anything taking longer than about a tenth of a second, you should do it in a background thread, so your UI doesn’t freeze up. Be sure to only interact with GUI elements from the main thread, or you can get segfaults. Python’s GIL isn’t a big issue here: the UI thread shouldn’t need much Python processing time.
  • Updates: Esky is a framework for updating frozen Python applications. I haven’t tried it, but it looks interesting.

Article taken from:

Python GUI Applications


PyGTK provides Python bindings for the GTK+ toolkit. Like the GTK+ library itself, it is currently licensed under the GNU LGPL. It is worth noting that PyGTK only currently supports the Gtk-2.X API (NOT Gtk-3.0). It is currently recommended that PyGTK not be used for new projects and that existing applications be ported from PyGTK to PyGObject.

PyGObject aka (PyGi)

PyGObject provides Python bindings, which gives access to the entire GNOME software platform. It is fully compatible with GTK+ 3. Here is a tutorial to get started with Python GTK+ 3 Tutorial.

API Reference


Kivy is a Python library for development of multi-touch enabled media rich applications. The aim is to allow for quick and easy interaction design and rapid prototyping, while making your code reusable and deployable.

Kivy is written in Python, based on OpenGL and supports different input devices such as: Mouse, Dual Mouse, TUIO, WiiMote, WM_TOUCH, HIDtouch, Apple’s products and so on.

Kivy is actively being developed by a community and is free to use. It operates on all major platforms (Linux, OSX, Windows, Android).

The main resource for information is the website:


Only available on OS X. Don’t pick this if you’re writing a cross-platform application.


PySide is a Python binding of the cross-platform GUI toolkit Qt.

pip install pyside


If your software does not fully comply with the GPL you will need a commercial license!

PyQt provides Python bindings for the Qt Framework (see below).

PyjamasDesktop (pyjs Desktop)

PyjamasDesktop is a port of Pyjamas. PyjamasDesktop is application widget set for desktop and a cross-platform framework. (After release v0.6 PyjamasDesktop is a part of Pyjamas (Pyjs)). Briefly, it allows the exact same Python web application source code to be executed as a standalone desktop application.

Python Wiki for PyjamasDesktop.

The main website; pyjs Desktop.


Qt is a cross-platform application framework that is widely used for developing software with a GUI but can also be used for non-GUI applications.


Tkinter is a thin object-oriented layer on top of Tcl/Tk. It has the advantage of being included with the Python standard library, making it the most convenient and compatible toolkit to program with.

Both Tk and Tkinter are available on most Unix platforms, as well as on Windows and Macintosh systems. Starting with the 8.0 release, Tk offers native look and feel on all platforms.

There’s a good multi-language Tk tutorial with Python examples at TkDocs. There’s more information available on the Python Wiki.


wxPython is a GUI toolkit for the Python programming language. It allows Python programmers to create programs with a robust, highly functional graphical user interface, simply and easily. It is implemented as a Python extension module (native code) that wraps the popular wxWidgets cross platform GUI library, which is written in C++.

Install (Stable) wxPythongo to and download the appropriate package for your OS.

Article original from:

6 Python Libs you will want to use.


What it is: Pyglet is cross-platform framework for multimedia and windows graphics in pure Python.

Why you need it: It provides handy access to items that are tedious to implement from scratch for a GUI application: window functions, OpenGL graphics, audio and video playback, keyboard and mouse handling, and working with image files. (It doesn’t provide UI widgets like buttons, toolbars, or menus, though.)

All of this is done through the native platform capabilities in Windows, OS X, or Linux, so there are no binary dependencies; it’s pure Python. It’s also BSD-licensed, so it can be included in any commercial or open source project.


What it is: Peewee is a small but powerful library for accessing databases by way of an ORM, with native support for SQLite, MySQL, and PostgreSQL.

Why you need it: Any application that uses external data in more than a trivial manner typically uses a database, but getting and setting data from a database via ad hoc connection strings is asking for trouble.

Peewee provides a safe, programmatic path to access database resources, using a set of Python classes that are intuitive for both Python developers and database engineers. With Peewee, a quick-and-dirty way to access a database can be later expanded to a more robust option without having to rip it out and start over. Transactions are natively supported, and optional modules provide support for everything from connection pooling to advanced field types like many-to-many.


What it is: Bottle is a tiny, lightweight Web framework that’s also quite fast.

Why you need it: When you simply want to throw together a quick RESTful API or use the bare bones of a Web framework to build an app, Bottle gives you no more than you need. Routing, templates, access to request and response data, support for multiple server types from plain old CGI on up, and support for more advanced features like WebSockets — it’s all here.

The amount of work needed to get started is minimal, and Bottle’s design is elegantly extensible for when more advanced functions have to be plugged in.


What it is: « Pythonic remote execution » — in plainer English, Invoke allows you to perform admin tasks using a Python library.

Why you need it: Who wouldn’t want a « clean, high-level API for running shell commands and defining/organizing task functions »? Using Python as a replacement for common shell scripting tasks makes sense, and Invoke provides common-sense solutions to take command-line tasks and manage them as if they were Python functions, allowing bigger items to be elegantly built around them.

Note that Invoke’s version as of this writing is considered pre-release software; if you want something guaranteed stable (if no longer being actively developed), consider Invoke’s predecessor, Fabric.


What it is: Splinter is a Python library for testing Web applications by automating interactions with them.

Why you need it: Let’s face it — little is less fun than trying to automate Web application testing. Splinter automates everything end to end, invoking the browser, passing URLs, filling out forms, clicking buttons, and so on.

It requires drivers to work with a specific browser, but Chrome and Firefox are already covered, and it can use Selenium Remote to control a browser running elsewhere. You can even manually execute JavaScript in the target browser.

Splinter is useful if you want to find out how specific browsers behave when confronted with a given website. For automating site interactions without a browser — essentially a kind of curl on steroids — check out Twill.


What it is: The Arrow library sorts out the mess that is Python’s date/time handling.

Why you need it: Dealing with time zones, date conversions, date formats, and all the rest are a headache and a half. With Python’s standard library for date/time work, you get two headaches.

Arrow provides four big boons, all useful in the short and the long term. One, it’s a drop-in replacement for Python’s datetime module, meaning common function calls like .now() and .utcnow() work as expected. Two, it provides methods for common needs like shifting or converting timezones. Three, it provides « humanized » date/time information — such as being able to say something happened « an hour ago » or will happen « in two hours » without a lot of effort. Four, it can localize date/time information without breaking a sweat.