在安装了32位python 2.7的64位系统中,我尝试执行以下操作:
import subprocess
p = subprocess.call('dir', shell=True)
print p
Run Code Online (Sandbox Code Playgroud)
但这给了我:
Traceback (most recent call last):
File "test.py", line 2, in <module>
p = subprocess.call('dir', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
如果我在终端做...
dir
Run Code Online (Sandbox Code Playgroud)
...当然会打印当前文件夹内容.
我试图将shell参数更改为shell = False.
编辑:其实我不能在路径上调用任何可执行文件subprocess.call().该声明p = subprocess.call('dir', shell=True)在另一台机器上工作正常,我认为它是相关的.
如果我做
subprocess.call('PATH', shell=True)
Run Code Online (Sandbox Code Playgroud)
然后我明白了
Traceback (most …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Visual sutdio 代码中使用 subprocess 模块,但我不断收到错误。这是代码:
import subprocess
subprocess.run("dir")
Run Code Online (Sandbox Code Playgroud)
或者
import subprocess
subprocess.run("dir","/p")
Run Code Online (Sandbox Code Playgroud)
错误是:
Traceback (most recent call last):
File "e:\Dropbox (Personal)\My Python\external\extrprg.py", line 2, in <module>
subprocess.run("dir")
File "E:\Users\klo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "E:\Users\klo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "E:\Users\klo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
我希望 python 运行dir并给我目录中的文件夹列表