6

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.

O. Schultz
  • 155
  • 1
  • 3
  • 12
  • Why not run the Python script all the time and let it check for new files periodically? – timgeb Mar 29 '18 at 13:33
  • I could also do that, but I don´t know how to check if there are new files in the folder, yet. – O. Schultz Mar 29 '18 at 13:42
  • http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html – CSJ Mar 29 '18 at 13:42
  • @CSJ This code is weird. "For common-sense reasons, dictionaries are used to hold the list of files each time round." I don't see the common sense here since alle the values are `None` (and never used). – timgeb Mar 29 '18 at 13:46
  • not sure the `None` used for, maybe can be improve it to record the timestamp the file last changed. and compare the timestamp too. just cite the url for you to reference the 3 ways he mentioned – CSJ Mar 29 '18 at 13:52
  • the first way is simple and direct which use `listdir` to compare file list in the path every time, although I think should compare about timestamp if you want to do something else if the content changed. – CSJ Mar 29 '18 at 13:55
  • Thanks for the advise so far CSJ. The code works as expected right now, I'm going to test a bit more after Eastern. – O. Schultz Mar 29 '18 at 14:53

6 Answers6

4

There are several ways to do this and also a special Python package for this

Watchdog Python script for Windows file system

https://pypi.python.org/pypi/watchdog

https://blog.philippklaus.de/2011/08/use-the-python-module-watchdog-to-monitor-directories-for-changes

Joe
  • 6,758
  • 2
  • 26
  • 47
2

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Faraaz Kurawle
  • 1,085
  • 6
  • 24
1

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!

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
1

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.

Matt_G
  • 506
  • 4
  • 14
0

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.

bguiz
  • 27,371
  • 47
  • 154
  • 243
0

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.