你試圖調用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內建的,不是二進制。 –