Python scripts (files with the extension .py) will be executed by
python.exe by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension .pyw which will cause the script to be
executed by pythonw.exe by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
import shutil, os, time
while True:
for filename in os.listdir('folderToMoveFrom'):
if filename.endswith((desired file extensions)):
shutil.move( (folderToMoveFrom + filename), folderToMoveTo)
time.sleep(6)
在Windows上,可以使用^{} 将python脚本作为后台进程运行:
例如
为了使脚本连续运行,可以使用帮助事件调度的^{} 模块:
^{pr2}$现在,要在Windows上终止后台进程,只需运行:
其中
processId
是要终止的进程的ID。在一种选择是更改脚本,使其能够连续运行而不是重复运行。只需把整个事情打包成一个while循环,然后加上睡眠。在
为了确保这是从计算机启动的,并在发生异常时提供自动重新启动,我建议使用非吸吮服务管理器将其放入Windows服务中(网址:www.nssm.cc). 这有几个步骤(参见文档),但一旦完成,您的脚本将只是另一个windows服务,您可以从标准启动和停止服务.msc实用性。在
我找到了一个有效的解决方案:
如果执行上述代码时没有时间。睡觉()函数由于嵌套在另一个“找不到文件”错误中的“找不到文件”错误,程序在新文件进入文件夹后崩溃。 不知道这是怎么回事,但我对我目前的情况很满意。 现在您只需要将脚本添加到任务调度器中,以便在Pythonw下运行,使其作为后台进程运行。或者不用运行脚本,只要记住为pythonw添加适当的指令,就可以运行批处理文件。当然,你只需要启动这个过程一次。在
相关问题 更多 >
编程相关推荐