首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python OSError:[Errno 2]

Python OSError:[Errno 2]
EN

Stack Overflow用户
提问于 2014-10-14 04:29:42
回答 1查看 269关注 0票数 1

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

代码语言:javascript
复制
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)

所报告的错误如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-14 04:42:41

追溯显示问题与cmd1有关。代码中的两个命令都不正确。

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

代码语言:javascript
复制
cmd1 = ['./webserver.py', port_str]
cmd2 = ['wget', 'localhost:%s/%s' % (port_str, fn_nospace)]

此外,OSError具有filenamefilename2属性,您可以通过编程方式检查这些属性。虽然在这种情况下没有设置它们

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26352671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档