我想我的运行自动化脚本蟒蛇时,才会有不同的文件夹中的代码更改。如何检测的变化和运行脚本?
您可以使用os.path.getmtime
知道什么时候该文件的最后修改,并不断检查这个时间。
事情是这样的:
import os.path, time
last_time = time.ctime(os.path.getmtime(file))
while(True):
if time.ctime(os.path.getmtime(file)) != last_time:
print('Script changed!')
#do what u want...
break
time.sleep(10)
你要监控目录中的文件的变化,我几乎不建议使用Watchdog。
看门狗自带的脚本watchmedo
它可以在文件变更运行shell脚本。
你可以尝试这样的事情:
watchmedo shell-command \
--patterns="*.py" \
--recursive \
--command='path/to/python path/to/script.py "${watch_src_path}"' \
your_directory