所以,我知道每个人都会告诉我要使用子流程模块,但是我不能在我正在进行的项目中使用它,因为Piping根本不想在我的系统上使用wxpython和py2exe。
所以,我一直在使用os.system调用。我需要知道如何等待这个过程结束。目前,我
os.system(cmd)我的命令执行起来可能需要很长时间,所以通常会提前超时。如何使我的程序等待os.system?我尝试过等待,我想这对os.system不起作用。
我正在为windows开发,所以很不幸我不能使用叉和execvp。我有很多手被绑着:
发布于 2013-07-26 19:27:12
您可以更正您的代码:
os.system('cmd')对子流程的额外解释:
import subprocess
ls_output = subprocess.check_output(['ls'])运行外部命令
要在不与外部命令交互的情况下运行外部命令,例如使用os.system(),请使用call()函数。
import subprocess
# Simple command
subprocess.call('ls -l', shell=True)$ python replace_os_system.py
total 16
-rw-r--r-- 1 root8085 root8085 0 Jul 1 13:27 __init__.py
-rw-r--r-- 1 root8085 root8085 1316 Jul 1 13:27 replace_os_system.py
-rw-r--r-- 1 root8085 root8085 1167 Jul 1 13:27 replace_os_system.py~# run cmd
import subprocess
l = subprocess.call(['cmd'])额外的例子:让一个系统调用三种不同的方式:
#! /usr/bin/env python
import subprocess
# Use a sequence of args
return_code = subprocess.call(["echo", "hello sequence"])
# Set shell=true so we can use a simple string for the command
return_code = subprocess.call("echo hello string", shell=True)
# subprocess.call() is equivalent to using subprocess.Popen() and wait()
proc = subprocess.Popen("echo hello popen", shell=True)
return_code = proc.wait() # wait for process to finish so we can get the return code控制标准和标准输出:
#! /usr/bin/env python
import subprocess
# Put stderr and stdout into pipes
proc = subprocess.Popen("echo hello stdout; echo hello stderr >&2", \
shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
# Read from pipes
for line in proc.stdout:
print("stdout: " + line.rstrip())
for line in proc.stderr:
print("stderr: " + line.rstrip())https://stackoverflow.com/questions/17868500
复制相似问题