0

Is it possible to have a script run on a file when it's created if it has a specific extension?

let's call that extension "bar"

if I create the file "foo.bar", then my script will run with that file as an input.

Every time this file is saved, it would also run on the file.

Can I do that? If yes, how? If not, why?

note: If there is some technicality of why this is impossible, but I can do very close, that works too!

Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128

2 Answers2

1

If you are using linux use pyinotify described on the website as follows: Pyinotify: monitor filesystem events with Python under Linux.

If you also want it to work using Mac OS X and Windows, you can have a look at this answer or this library.

Community
  • 1
  • 1
lc2817
  • 3,722
  • 16
  • 40
  • this is meant for a plug-in...is there any way to do this without forcing users to download something – Ryan Saxe Sep 19 '13 at 21:49
  • You are going to have the users executing a python script anyway isn't it ? So you can add this library and use it in your code, isn't it? – lc2817 Sep 19 '13 at 21:51
  • I'm actually not. This is for a parser on a specific kind of dynamic text file that is unrelated to python. The parser is written in python though – Ryan Saxe Sep 19 '13 at 21:52
  • I don't understand, you don't want to do any code and still have this thing working on any platform? – lc2817 Sep 19 '13 at 21:55
  • the parser is written. I want when someone saves a file with a given extension, to run that parser on that file. does that make sense? – Ryan Saxe Sep 19 '13 at 21:57
  • Then have another script monitoring the file creation/modification event using the library that I suggested. When it detects an event it should call the parser. – lc2817 Sep 19 '13 at 21:58
  • I know, that's what this question is asking. Is there any way to do this without requiring my users to download something – Ryan Saxe Sep 19 '13 at 22:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37697/discussion-between-lc2817-and-ryan-saxe) – lc2817 Sep 19 '13 at 22:03
  • They already have to download something. Just include the library in what you give them. – kindall Sep 19 '13 at 22:10
0

You could do what Werkzeug does (this code copied directly from the link):

def _reloader_stat_loop(extra_files=None, interval=1):
    """When this function is run from the main thread, it will force other
    threads to exit when any modules currently loaded change.

    Copyright notice.  This function is based on the autoreload.py from
    the CherryPy trac which originated from WSGIKit which is now dead.

    :param extra_files: a list of additional files it should watch.
    """
    from itertools import chain
    mtimes = {}
    while 1:
        for filename in chain(_iter_module_files(), extra_files or ()):
            try:
                mtime = os.stat(filename).st_mtime
            except OSError:
                continue

            old_time = mtimes.get(filename)
            if old_time is None:
                mtimes[filename] = mtime
                continue
            elif mtime > old_time:
                _log('info', ' * Detected change in %r, reloading' % filename)
                sys.exit(3)
        time.sleep(interval)

You'll have to spawn off a separate thread (which is what Werkzeug does), but that should work for you if you if you don't want to add pyinotify.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290