{Python 3.5/3.6
之后出现的。另外Python 2.7
安装在我的电脑(windows 10计算机)上
import subprocess
out = subprocess.check_output(['dir'])
错误消息:
File "C:\Python36\lib\subprocess.py", line 336, in check_output **kwargs).stdout
File "C:\Python36\lib\subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process:
File "C:\Python36\lib\subprocess.py", line 707, in init restore_signals, start_new_session)
File "C:\Python36\lib\subprocess.py", line 990, in _execute_child startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
它不是可执行文件,而是shell的built-in。Python子流程模块找不到它,因此出现错误
如果您想使用子流程模块,请使用一些现有的二进制文件,例如} 或^{}
python
、notepad
或ping
。 如果您需要列出文件夹内容,请使用^{似乎“dir”不在您的路径中。我不知道此可执行文件在Windows上的完整路径,但也许您应该将dir替换为c:\windwos\system\dir
或者,最好的解决方案是使用操作系统模块中的函数列出目录:
除了@grundic
如果确实要执行cmd内置命令,则必须在本例中执行
cmd.exe /c COMMAND_HERE
:/c
表示执行后cmd.exe
关闭相关问题 更多 >
编程相关推荐