[英]How to make python wait untill an ongoing process is completed
我正在编写一个 Python 脚本来将文件夹中的文件组织到各个目录。 我希望它连续运行,所以我使用while
循环来扫描文件。 但是如果我尝试在文件夹中粘贴一个新文件,我的脚本将终止说The process cannot access the file because it is being used by another process
。 我知道它因为粘贴文件的过程而停止了。 现在我希望我的脚本等到正在进行的进程停止
代码示例:
while(True):
scan()
#rest of commands
所以我希望scan()
function 等到进程结束
提前致谢
使用 try/except 语句。 如果scan()
function 以错误结束,则打印一些消息并继续尝试。 您也可以pass
异常和 go on。 代码应该是这样的:
while(True):
try:
scan()
#rest of commands
except:
# error message or pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.