尝试创建测试文件以同时运行两个进程。使用子进程模块执行此操作。有一个问题,它说文件无法找到,但它没有指定哪个文件。我们使用绝对文件名,可以确认路径是否正确。谁能告诉我们这个默默无闻的含义?
import subprocess
import random
port_num = random.randint(1049, 2000) # generates random port number
fns = open("filenames.txt").readlines() #reads in the file names
port_str = str(port_num)
for fn in fns:
fn_nospace = fn.strip() #remove any excess spaces
print fn_nospace
cwdhalf = ['pwd']
subprocess.call(cwdhalf)
cmd1 = ['./webserver.py '+port_str] # open webserver with the input portnumber
subprocess.check_call(cmd1) # calls cmd1
cmd2 = ['wget localhost:'+port_str+'/'+fn_nospace] # samething with wget, only this time using the filename
subprocess.check_call(cmd2)
报告的错误如下:
Traceback (most recent call last):
File "testwebserver.py", line 26, in <module>
subprocess.check_call(cmd1) # calls cmd1
File "/usr/lib/python2.6/subprocess.py", line 493, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.6/subprocess.py", line 480, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
答案 0 :(得分:0)
回溯显示问题出在cmd1
。代码中的两个命令都不正确。
规则很简单:一个列表项 - 一个命令行参数:
cmd1 = ['./webserver.py', port_str]
cmd2 = ['wget', 'localhost:%s/%s' % (port_str, fn_nospace)]
此外,OSError具有filename
,filename2
属性,您可以通过编程方式进行检查。 Though they are not set in this case