When should I use Popen() and when should I use call()?
Caveat: new to Python.
Wanting to hear from professionals who actually use it:
What are the main differences between subprocess.Popen()
and subprocess.call()
and when is it best to use each one?
Unless you want to read why I was thinking about this question or what to center your answer around, you may stop reading now.
I was inspired to ask this question because I am working through an issue in a script where I s开发者_如何学运维tarted using subprocess.Popen()
, eventually called a system pause, and then wanted to delete the .exe that created the system pause, but I noticed with Popen()
, the commands all seemed to run together (the delete on the .exe gets executed before the exe is closed..), though I tried adding communicate()
.
Here is fake code for what I'm describing above:
subprocess.Popen(r'type pause.exe > c:\worker.exe', shell=True).communicate()
subprocess.Popen(r'c:\worker.exe', shell=True).communicate()
subprocess.Popen(r'del c:\worker.exe', shell=True).communicate()
subprocess.call(*popenargs, **kwargs) Run command with arguments. Wait for command to complete, then return the returncode attribute.
If you create a Popen
object, you must call the sp.wait()
yourself.
If you use call
, that's done for you.
精彩评论