Ubuntu – Run python script when a file has been added to a folder

bashcommand linedirectorypythonscripts

Is it possible to run a Python script when a file has been added to a folder?

In my scenario I have a folder that contains movies and the Python script gets the details (name, year, plot, rating, etc) from OMDb and puts them into a file, which MySQL uses.

Every time I add a movie I have to run the Python script.

I want to know if it is possible to automatically run the Python script when a file has been added to the folder?

Thanks in advance.

Best Answer

Considering a unique folder where you add files often.

  • Build a script (Python and Bash are good options) that loops for folder size every 'n' seconds (or minutes, how often do you add files?).

  • Whenever a new file is created, the size will change and the script should trigger a function that search the last file added and get necessary file info and launches your Python script.

You could easily integrate the above descriptions to your existing Python script.

If you have several folders, you could make it recursive.

Let me know if you can't realize how to do it and I will help you.

Good luck!