Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Watch files and automatically run a script in Linux
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 -w foo.txt bar.png baz.pdf --
Then the script is executed every time one of the files mentioned on the command line is changed on disk. Wink I typically use it while developing 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-*-
__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 = or []
    sys.argv[1:] = ['auto',]
    import doit

    import doit

Your link leads to a private gist/repo, so we can't actually see the file :p
(Jan-23-2018, 05:13 PM)nilamo Wrote: Your link leads to a private gist/repo, so we can't actually see the 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).
Oh lol. Yes, I see what you shared in the gist.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  web crawler with Tor proxy & Firefox ESR on Linux Kali rootVIII 0 157 Jul-01-2019, 04:02 AM
Last Post: rootVIII
Lightbulb Script broke with Python 3 / improve script for printing aspecteleven 4 490 Feb-22-2019, 12:23 PM
Last Post: buran
  The Python Tkinter Youtube Audio Downloader for Linux rootVIII 0 454 Dec-19-2018, 11:33 PM
Last Post: rootVIII
  Getting wanted data from the 'top' command (Linux) rootVIII 7 825 Nov-19-2018, 09:41 PM
Last Post: DeaD_EyE
  Find a local free port to start a server in linux Gribouillis 0 411 Oct-14-2018, 08:00 PM
Last Post: Gribouillis
  Linux user folder names (if translated) Axel_Erfurt 0 444 Sep-23-2018, 05:55 PM
Last Post: Axel_Erfurt - process trace back for linux Skaperen 3 871 Feb-06-2018, 08:56 AM
Last Post: Gribouillis
  ATA Pass-Through library for linux kazenniy 0 1,174 Nov-07-2016, 08:36 AM
Last Post: kazenniy
  msvcrt.getkey for linux metulburr 0 4,786 Nov-03-2016, 08:18 PM
Last Post: metulburr
  script to filter python files Skaperen 3 1,704 Nov-03-2016, 02:41 AM
Last Post: Skaperen

Forum Jump:

Users browsing this thread: 1 Guest(s)