Python中subprocess.call没有此类文件或目录错误
通常,我尝试使用Bash而不是Python从命令行读取数据,这样我就有了制表符完成功能。我想用最简单的方法来做这件事。但是,我很难让下面的代码正常工作,我想了解是什么导致了这个问题 Python脚本:Python中subprocess.call没有此类文件或目录错误,python,bash,popen,tab-completion,Python,Bash,Popen,Tab Completion,通常,我尝试使用Bash而不是Python从命令行读取数据,这样我就有了制表符完成功能。我想用最简单的方法来做这件事。但是,我很难让下面的代码正常工作,我想了解是什么导致了这个问题 Python脚本: from subprocess import call call(['read', '-ep', 'Path:', 'temporaryPath']) print temporaryPath 错误回溯: Traceback (most recent call last): File "tmp
from subprocess import call
call(['read', '-ep', 'Path:', 'temporaryPath'])
print temporaryPath
错误回溯:
Traceback (most recent call last):
File "tmp.py", line 2, in <module>
call(['read', '-ep', 'Path:', 'temporaryPath'])
File "/usr/lib64/python2.6/subprocess.py", line 478, in call
p = Popen(*popenargs, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
回溯(最近一次呼叫最后一次):
文件“tmp.py”,第2行,在
调用(['read'、'-ep'、'Path:'、'temporaryPath'])
文件“/usr/lib64/python2.6/subprocess.py”,第478行,在调用中
p=Popen(*popenargs,**kwargs)
文件“/usr/lib64/python2.6/subprocess.py”,第642行,在__
错误读取,错误写入)
文件“/usr/lib64/python2.6/subprocess.py”,第1238行,在_execute_child中
引发子对象异常
OSError:[Errno 2]没有这样的文件或目录
您正在尝试调用read
,这是一个shell内置:
$ type read
read is a shell builtin
这个特殊的shell内置程序没有等效程序:
$ which read
$
因此,根据strace
,Python将无法在PATH
环境变量中找到它:
[pid 17266] execve("/usr/local/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
[pid 17266] execve("/usr/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
[pid 17266] execve("/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
[pid 17266] execve("/usr/local/games/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
[pid 17266] execve("/usr/games/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
[…]
[pid 17266] write(4, "OSError:", 8 <unfinished ...>
您现在遇到了一个新问题:shell内置的read
将读取的值存储为shell变量,该变量将在调用subprocess.call
后立即消失
哦,在
阅读
shell内置中,您也没有完成。如果您想以交互方式向用户询问某个问题,或者如果不需要交互,您可能只需要使用来解析用户作为命令行参数给出的内容,这样用户在键入参数时会有一些shell完成,通常不在标志上,因为用户shell不知道这些参数,但是在路径上。read
是bash内置的,而不是二进制文件。感谢您向我展示argparse,它看起来是实现我想要的功能的一种非常健壮的方法。另外,我现在明白了为什么调用需要shell=True
,但为什么下面的代码不起作用call('read-ep\'路径:\'temporaryPath;export temporaryPath',shell=True)
export
不允许您修改调用进程的环境。任何东西都不允许您修改调用进程的env。但是您仍然可以echo$temporalypath
,并从进程stdout获取值(不要这样做,最好使用input
,甚至更好地使用argparse
)。啊,现在这看起来很明显。谢谢
$ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('read', shell=True)
/bin/sh: 1: read: arg count
2
>>> subprocess.call('read foo', shell=True)
hello world
0