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
#2
Quote:
    import doit
    doit.run(globals())

Your link leads to a private gist/repo, so we can't actually see the doit.py file :p
Reply
#3
(Jan-23-2018, 05:13 PM)nilamo Wrote: Your link leads to a private gist/repo, so we can't actually see the doit.py file :p
The module doit is on pypi, it's not mine! You can install it with pip3 or perhaps with your OS' software manager. Can you see and download my script in the gist? (it is the same as the one I posted here).
Reply
#4
Oh lol. Yes, I see what you shared in the gist.
Reply
#5
2023 UPDATE: This old script still works but modern versions of 'doit' don't accept the parameter 'auto' on the command line. The solution is to install also 'doit-auto1', so prior to using this script, type
Output:
python -m pip install doit python -m pip install doit-auto1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  script to filter python files Skaperen 3 44,159 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