何时应使用Popen(),何时应使用call()?

2024-04-19 04:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

注意:Python新手。在

想听听真正使用它的专业人士的意见:

subprocess.Popen()和{}之间的主要区别是什么?什么时候最好使用它们?

除非你想知道我为什么在想这个问题,或者你的答案围绕着什么,你现在可以停止阅读了。在

我被启发提出这个问题是因为我正在解决一个脚本中的一个问题,我开始使用subprocess.Popen(),最后调用了系统暂停,然后想删除创建系统暂停的.exe,但我注意到Popen(),虽然我尝试添加communicate(),但所有命令似乎都是一起运行的(在关闭exe之前执行对.exe的删除操作..)。在

下面是我上面描述的伪代码:

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()

Tags: 答案命令脚本true系统shellexe意见
1条回答
网友
1楼 · 发布于 2024-04-19 04:25:21

subprocess.call(*popenargs, **kwargs) Run command with arguments. Wait for command to complete, then return the returncode attribute.

如果创建Popen对象,则必须自己调用sp.wait()。在

如果您使用call,那么就完成了。在

相关问题 更多 >