#!/usr/bin/env python

import os
import sys
from os.path import getmtime

# Parse script arguments and configuration files.
# ...

WATCHED_FILES = [__file__]
WATCHED_FILES_MTIMES = [(f, getmtime(f)) for f in WATCHED_FILES]

while True:
    # Wait for inputs and act on them.
    # ...

    # Check whether a watched file has changed.
    for f, mtime in WATCHED_FILES_MTIMES:
        if getmtime(f) > mtime:
            # One of the files has changed, so restart the script.
            print('--> restarting')
            os.execv(__file__, sys.argv)

#!/usr/bin/env python

import os
import sys
from os.path import getmtime

# Parse script arguments and configuration files.
# ...

WATCHED_FILES = [__file__]
WATCHED_FILES_MTIMES = [(f, getmtime(f)) for f in WATCHED_FILES]

while True:
    # Wait for inputs and act on them.
    # ...

    # Check whether a watched file has changed.
    for f, mtime in WATCHED_FILES_MTIMES:
        if getmtime(f) != mtime:
            # One of the files has changed, so restart the script.
            print('--> restarting')
            os.execv(__file__, sys.argv)

Use '!=' instead of '>' when checking for mtime changes.
blog/en-2014-03-23: Use '!=' instead of '>' when checking for mtime changes.

The use of '!=' ensures that the reloading works even when the new mtime is
lower than the current mtime (e.g. when we use a backed-up version of a file).

Thanks to Ronald for pointing this out
(http://blog.petrzemek.net/2014/03/23/restarting-a-python-script-within-itself/#comment-668).
