Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python操作系统错误:[Errno 2]_Python_Networking_Subprocess - Fatal编程技术网

Python操作系统错误:[Errno 2]

Python操作系统错误:[Errno 2],python,networking,subprocess,Python,Networking,Subprocess,尝试创建一个测试文件以同时运行两个进程。使用子流程模块执行此操作。有一个问题,它说找不到文件,但没有指定哪个文件。我们使用的是绝对文件名,可以确认路径是否正确。有谁能告诉我们这种默默无闻可能是什么 import subprocess import random port_num = random.randint(1049, 2000) # generates random port number fns = open("filenames.txt").readlines() #reads i

尝试创建一个测试文件以同时运行两个进程。使用子流程模块执行此操作。有一个问题,它说找不到文件,但没有指定哪个文件。我们使用的是绝对文件名,可以确认路径是否正确。有谁能告诉我们这种默默无闻可能是什么

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

回溯显示问题出在cmd1上。代码中的两个命令都不正确

规则很简单:一个列表项-一个命令行参数:

cmd1 = ['./webserver.py', port_str]
cmd2 = ['wget', 'localhost:%s/%s' % (port_str, fn_nospace)]
此外,OSError具有filename、filename2属性,您可以通过编程方式进行检查

call和check_调用都会等待相应的子进程完成。如果要并行运行进程,请使用Popen。