如何在os.system完成之前将执行延迟? [英] How can I delay execution until after os.system finishes?
问题描述
我正在使用os.system
将文件从系统复制到另一个.一个非常简单的程序的逻辑是在复制此文件后执行另一组命令.
I am using os.system
to copy a file from a system to another. The logic of a very simple program is to execute another set of commands after this file gets copied.
问题在于,os.system
实际上并不等待文件被复制,而是开始执行下一行.这会导致系统出现问题.我实际上可以通过time.sleep()
提供一些等待功能,但是我们必须复制文件大小从500 MB到有时20 GB的文件,并且所花费的时间是非常不同的.
The problem is that os.system
does not actually wait for the file to be copied, and gets to executing the next line. This causes issues to the system. I could actually give some wait functions, through time.sleep()
, but we have to copy files with sizes ranging from 500 MB to sometimes 20 GB, and the times taken are very different.
有什么解决方案?我需要以某种方式告诉我的程序文件已复制,然后执行下一行.
What's the solution? I need to somehow tell my program that the files are copied, and then to execute the next line.
推荐答案
我要尝试的第一件事是使用 shutil.copyfile()
而不是外部程序来复制文件.如果必须使用外部程序,则应通过 subprocess.Popen()
来调用它a>,而不是通过os.system()
.您可以使用Popen.wait()
等待子进程完成.
The first thing I'd try is to use shutil.copyfile()
instead of an external program to copy the file. If you have to use an external program, you should call it via subprocess.Popen()
, not via os.system()
. You can use the Popen.wait()
to wait for the subprocess to finish.
这篇关于如何在os.system完成之前将执行延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!