我正在尝试在可视化的sutdio代码中使用子流程模块,但始终出现错误。这是代码:
import subprocess
subprocess.run("dir")
或
import subprocess
subprocess.run("dir","/p")
,错误是:
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
我希望python运行dir
并给我目录中的文件夹列表
答案 0 :(得分:0)
dir
是cmd.exe
中的命令,这意味着您要执行以下操作:
subprocess.run(['cmd.exe', '/c', 'dir'])