subprocess.run在Windows中不起作用-系统找不到指定的文件

时间:2019-05-12 15:58:42

标签: python subprocess

我正在尝试在可视化的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并给我目录中的文件夹列表

1 个答案:

答案 0 :(得分:0)

dircmd.exe中的命令,这意味着您要执行以下操作:

subprocess.run(['cmd.exe', '/c', 'dir'])