How can I automatically run a python script, whenever a Word-File is added to a specific folder? This python script would need to work with the Word-File afterwards. My operating system is Windows.
Thanks for your help in advance.
How can I automatically run a python script, whenever a Word-File is added to a specific folder? This python script would need to work with the Word-File afterwards. My operating system is Windows.
Thanks for your help in advance.
There are several ways to do this and also a special Python package for this
Watchdog Python script for Windows file system
I have tried all these solutions, but it didnt worked well for me
But!
I have got the solution,
Heres the code:
path_to_watch = "your/path"
print('Your folder path is"',path,'"')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
if added:
print("Added: ", ", ".join (added))
break
else:
before = after
I have edited the code, the orginal code is available at http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
The original code was made in python 2x so you need to convert it in python 3.
Note:
Whenever you add any file in path, it prints the text and breaks, and if no files are added then it would continue to run.
How can I automatically run a python script, whenever a Word-File is added to a specific folder?
A solution for this can be found in the following code:
import os
# define path for file
word_file = "path/to/file"
while True:
if os.path.exists(word_file):
# your code here
This code uses the os module to interact with the PC's file system, and starts a while loop that always runs. The conditional statement's condition is the existence of the word_file
in the specified location, and you can execute your desired code within the if statement.
I hope this is helpful - let me know if you have any questions!
You may want to look into creating a python service.
How do you run a Python script as a service in Windows?
Like the other answer here, you will create an enduring loop to periodically check the filesystem for your file and then do something if/when it finds it.
you can use watchdog, and create your own eventHandler
, like, overwriting original functions inside filesystemeventhandler
, by creating a class using filesystemeventhandler(class MyHandler(fileSystemEventHandler):)
, you can change on_any_event or on_created_ or on_modified.
If you want to run this script infinite you can use this one:
import os
path_to_watch = "C:/Users/someone/"
print('Your folder path is"',path_to_watch,'"')
old = os.listdir(path_to_watch)
print(old)
while True:
new = os.listdir(path_to_watch)
if len(new) > len(old):
newfile = list(set(new) - set(old))
print(newfile[0])
old = new
extension = os.path.splitext(path_to_watch + "/" + newfile[0])[1]
if extension == ".zip":
print("hello")
else:
continue
else:
continue
In this case, I was monitoring for a new .zip file. But when you change it into your wanted extension, it will run something when there's a new word document. When you put the script you want to run when there is a new word document under the "if extension..." it should work.
Code is based on the script from @Faraaz Kurawle.