你试图调用read
这是一个shell内建:
$ type read
read is a shell builtin
和内置有这种特殊的外壳没有同等课程:
$ which read
$
所以Python将无法找到它在你的PATH
环境变量,根据strace
:
[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 ...>
但是,如果你明确要求Python的使用shell来执行你的命令,外壳本身将能够运行其内置read
:
$ 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
您现在有一个新的问题:内置read
被存储读取值作为一个shell变量的外壳,这将diseapear无线在呼叫subprocess.call
之后,外壳正在死亡。
哦,在read
shell内建中,你也没有完成。如果您想交互式地向用户提问,或者如果不需要交互,只需使用argparse解析用户作为命令行参数提供的内容即可,您应该只使用input,这样用户在键入命令行参数时就会有一些shell完成参数通常不在标志上,因为用户shell不知道它们,但是在路径上。
'read'是bash内建的,不是二进制。 –