Python的脚本的自动化应该运行时在文件中更改任何代码

问题描述 投票:2回答:2

我想我的运行自动化脚本蟒蛇时,才会有不同的文件夹中的代码更改。如何检测的变化和运行脚本?

python automation pycharm
2个回答
1
投票

您可以使用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)

1
投票

你要监控目录中的文件的变化,我几乎不建议使用Watchdog

看门狗自带的脚本watchmedo它可以在文件变更运行shell脚本。

你可以尝试这样的事情:

watchmedo shell-command \
    --patterns="*.py" \
    --recursive \
    --command='path/to/python path/to/script.py "${watch_src_path}"' \
    your_directory
© www.soinside.com 2019 - 2024. All rights reserved.