Python Forum
Watch files and automatically run a script in Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Watch files and automatically run a script in Linux
#1
The following script (download it here) named autopython3 runs a python script every time it is changed on disk. It also supports watching other files and running the script when one of these files is changed.

The script is based on the AWESOME module doit (according to doit's documentation, this script should work only in LINUX and MAC OSX). Ubuntu users can install it with
sudo apt install python3-doit
In order to run the below script, use a command line such as
autopython3 myprog.py
autopython3 -w foo.txt bar.png baz.pdf -- myprog.py
Then the script myprog.py is executed every time one of the files mentioned on the command line is changed on disk. Wink I typically use it while developing myprog.py or one of the watched files, in order to have immediate feedback from python every time I type Ctrl-s in my editor to save the file I'm working on.

#!/usr/bin/env python3
# -*-coding: utf8-*-
'''doc
'''
__version__ = '0.1.0'

from argparse import ArgumentParser
import sys

def task_AutoExecution():
    """my doc"""
    yield {
        'basename': 'AutoExecution',
        'actions': ['PYTHONIOENCODING="utf8" {python} {script}'.format(python=sys.executable, script=SCRIPT)],
        'watch': [PROG] + WATCH,
        'verbosity': 2,
        }
 
if __name__ == '__main__':
    parser = ArgumentParser(description='Automatically runs a python script every time it is changed on disk')
    parser.add_argument('script', metavar='SCRIPT', help='python script to execute', action='store')
    parser.add_argument('-w', '--watch', metavar='FILE', help='additional file to watch (end arglist with --)', nargs='*')
    args = parser.parse_args()
    SCRIPT = args.script
    PROG = SCRIPT.strip().split()[0]
    WATCH = args.watch or []
    sys.argv[1:] = ['auto',]
    import doit
    doit.run(globals())
Reply


Messages In This Thread
Watch files and automatically run a script in Linux - by Gribouillis - Jan-20-2018, 03:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  script to filter python files Skaperen 3 59,917 Nov-03-2016, 02:41 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020