Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python无法使用find执行windows命令_Windows_Python 3.x_Cmd_Subprocess_Call - Fatal编程技术网

python无法使用find执行windows命令

python无法使用find执行windows命令,windows,python-3.x,cmd,subprocess,call,Windows,Python 3.x,Cmd,Subprocess,Call,我要执行以下命令: dir c: | find "File" 获取文件夹中的文件数 奈索尔 subprocess.call(['dir c: | find "File"'], shell=True) 或 工作 Python将命令转换为 '"dir c: | find \"file\""' 导致了失败 有什么解决办法吗 相关说明: 谢谢如果您使用的是shell=True,您需要以字符串形式传递命令,该字符串在执行过程中由shell解释(例如Windows上的cmd)。仅当shell=Fals

我要执行以下命令:

dir c: | find "File"
获取文件夹中的文件数

奈索尔

subprocess.call(['dir c: | find "File"'], shell=True)

工作

Python将命令转换为

'"dir c: | find \"file\""'
导致了失败

有什么解决办法吗

相关说明:


谢谢

如果您使用的是
shell=True
,您需要以字符串形式传递命令,该字符串在执行过程中由shell解释(例如Windows上的
cmd
)。仅当
shell=False
时,以列表形式传递参数才适用

试着简单地说:

subprocess.call('dir c: | find "File"', shell=True)

如果您使用的是
shell=True
,则需要将命令作为字符串传递,该字符串在执行期间由shell解释(例如,Windows上的
cmd
)。仅当
shell=False
时,以列表形式传递参数才适用

试着简单地说:

subprocess.call('dir c: | find "File"', shell=True)

首先,我无法从windows中找到'dir'命令,因此无法使用'shell=False'执行该命令。 但它与

os.chdir('C:')
cdirszbuf = os.popen('dir | find "File"')
cdirinfo = cdirszbuf.readlines()

首先,我在windows中找不到'dir'命令,因此无法使用'shell=False'执行该命令。 但它与

os.chdir('C:')
cdirszbuf = os.popen('dir | find "File"')
cdirinfo = cdirszbuf.readlines()

谢谢

不幸的是,如果shell=false,它将失败,并出现错误:FileNotFoundError:[WinError 2]系统无法找到该文件specified@69444091,但是Łukasz告诉您使用
shell=True
,除了使用字符串而不是列表
subprocess.list2cmdline
不知道如何将参数列表转换为cmd shell命令行。至于获取
FileNotFoundError
dir
是cmdshell中的内置命令。使用
where.exe
命令来确定命令是外部可执行文件(例如
xcopy.exe
)还是内置于shell中(例如
copy
)。不幸的是,如果shell=false,失败,错误为:FileNotFoundError:[WinError 2]系统找不到该文件specified@69444091,但Łukasz告诉您使用
shell=True
,除非使用字符串而不是列表
subprocess.list2cmdline
不知道如何将参数列表转换为cmd shell命令行。至于获取
FileNotFoundError
dir
是cmdshell中的内置命令。使用
where.exe
命令确定一个命令是外部可执行文件(例如
xcopy.exe
)还是内置在shell中(例如
copy
)。在Python 3
os.popen
中,只需使用
shell=True
调用
subprocess.popen
,并将结果封装在类似文件的类中。另外,没人告诉过你使用
shell=False
,我已经解释过
dir
是一个内置的shell命令。没有要执行的
dir.exe
。为此,您必须使用shell,并支持命令行语法,例如在两个命令之间创建管道的
“|”
。在Python 3中,os.popen只需使用
shell=True
调用
subprocess.popen
,并将结果封装在类似文件的类中。另外,没人告诉过你使用
shell=False
,我已经解释过
dir
是一个内置的shell命令。没有要执行的
dir.exe
。为此,您必须使用shell,并支持命令行语法,例如用于在两个命令之间创建管道的
“|”