无法使用subprocess.Popen运行windows命令



我试图运行一个简单的windows命令dir E:ProjectsML /s /b使用subprocess.Popen函数而不使用Shell=True,我的示例程序如下

import subprocess
import os
import time
input='E:ProjectsML'
command = "dir "+input+' /s /b '
os.system(command)
status=subprocess.Popen(command.split(' '),shell=False,text=True,cwd=input,stdout=subprocess.PIPE)
while status.wait()!=0:
time.sleep(1)
print(status.communicate())

os.system()命令给出适当的结果,但与Popen()我面临以下问题

Traceback (most recent call last):
File "e:ProjectsArchiveViewerPythonR&DEncriptionWithPopenrunLocalCMD.py", line 9, in <module>
status=subprocess.Popen(command.split(' '),shell=False,text=True,cwd=input,stdout=subprocess.PIPE)
File "C:UsersnmaiyaAppDataLocalProgramsPythonPython39libsubprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:UsersnmaiyaAppDataLocalProgramsPythonPython39libsubprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

我知道,如果我设置shell=True命令将运行,但是有什么原因为什么它不工作与shell=Fales

dir是cmd.exe中的内部命令,没有dir.exe可以作为进程启动。

system("xyz")的内部执行方式类似于Windows上的cmd.exe /C xyz

最新更新