Listening to a target directory and executing a python file dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Heshan Suri
    New Member
    • Apr 2008
    • 6

    Listening to a target directory and executing a python file dynamically

    Hi,
    Assume that I have a target directory, which I am using as a location to store my python scripts.What I need to do is to listen to that directory and when a python script is placed in that directory, I need to execute it at that time.Can anyone give me an idea on how to do this using python?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Let's see... a python file monitoring daemon. First off I would suggest googling "python daemon" and following one of many tutorials that come up to create a daemon (a little bit beyond the scope of a single post).

    Then all you would need to do is have the daemon monitor the files in a certain directory:
    [code=python]
    >>> # during daemon init:
    >>> import os, time
    >>> myFilePath = os.path.join('C :\\', 'Documents and Settings', 'Administrator' , 'Desktop', 'pythtests')
    >>> myFilePath
    'C:\\Documents and Settings\\Admin istrator\\Deskt op\\pythtests'
    >>> if os.path.isdir(m yFilePath):
    ... origFileList = os.listdir(myFi lePath)
    ...
    >>> # Now during daemon's running process...
    >>> time.sleep(10) # Just wait a little while, or you can continuously monitor
    >>> fileList = os.listdir(myFi lePath)
    >>> if fileList != origFileList:
    ... for fileName in fileList:
    ... if fileName not in origFileList and fileName.endwit h('.py'):
    ... os.popen('%s' % os.path.join(my FilePath, fileName))
    ...
    >>> [/code]
    That's just a quick example to give you an idea of what to do... there are certainly better ways to achieve this, but at least to get you started...

    Comment

    Working...