尝试创建一个测试文件以同时运行两个进程。使用子流程模块这样做。有一个问题,它说找不到文件,但它没有指定哪个文件。我们使用绝对文件名,并可以确认路径是正确的。有人能告诉我们这种默默无闻是什么吗?
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发布于 2014-10-14 04:42:41
追溯显示问题与cmd1有关。代码中的两个命令都不正确。
规则很简单:一个列表项--一个命令行参数:
cmd1 = ['./webserver.py', port_str]
cmd2 = ['wget', 'localhost:%s/%s' % (port_str, fn_nospace)]此外,OSError具有filename、filename2属性,您可以通过编程方式检查这些属性。虽然在这种情况下没有设置它们。
https://stackoverflow.com/questions/26352671
复制相似问题