将文件添加到文件夹时,如何自动运行python脚本?

O. *_*ltz 5 python windows directory

每当将 Word 文件添加到特定文件夹时,如何自动运行 python 脚本?此 python 脚本之后需要使用 Word 文件。我的操作系统是 Windows。

提前感谢您的帮助。

Jar*_*rth 1

每当将 Word 文件添加到特定文件夹时,如何自动运行 python 脚本?

可以在以下代码中找到该问题的解决方案:

import os 

# define path for file
word_file = "path/to/file"

while True: 

    if os.path.exists(word_file):
        # your code here 
Run Code Online (Sandbox Code Playgroud)

这段代码使用os模块与 PC 的文件系统交互,并启动一个始终运行的while循环。条件语句的条件是word_file指定位置是否存在,您可以在if语句中执行您想要的代码。

我希望这对您有所帮助 - 如果您有任何疑问,请告诉我!