简体   繁体   English

通过打开 cmd 提示符运行命令时子进程模块不返回 output

[英]Subprocess module not returning output when running command by opening cmd prompt

I am using subprocess module to open command prompt and run few command in it and retrive the result.我正在使用 subprocess 模块打开命令提示符并在其中运行一些命令并检索结果。

I tried below code and it is not returning any output.我尝试了下面的代码,它没有返回任何 output。 Simply running for a long time.Please help what is wrong with this code.只是运行了很长时间。请帮助此代码有什么问题。 Similar to this code, i used notepad application path and text file path and it opened notepad application with text file.与此代码类似,我使用记事本应用程序路径和文本文件路径,并使用文本文件打开记事本应用程序。 But command prompt is not opening.但是命令提示符没有打开。

import subprocess
process = subprocess.Popen(['C:\\Windows\\system32\\cmd.exe', 'dir'], stdout=subprocess.PIPE)
stdout=process.communicate()
print(stdout)

The issue is that cmd.exe dir doesn't actually run the dir command.问题是cmd.exe dir实际上并没有运行 dir 命令。 You need to specify the /c flag, per MSDN .您需要根据MSDN指定/c标志。 So you really want所以你真的想要

import subprocess
process = subprocess.Popen(['C:\\Windows\\system32\\cmd.exe', '/c', 'dir'], stdout=subprocess.PIPE)
stdout=process.communicate()
print(stdout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM