用子进程包装 cmd.exe

发布于 2024-08-17 08:44:52 字数 314 浏览 2 评论 0原文

我尝试使用以下程序在Windows下包装cmd.exe,但它不起作用,它似乎在等待某些东西并且不显示任何内容。知道这里出了什么问题吗?

import subprocess

process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)  
process.stdin.write("dir\r\n")  
output = process.stdout.readlines()  
print output

I try to wrap cmd.exe under windows with the following program but it doesn't work , it seems to wait for something and doesn't display anything. Any idea what is wrong here ?

import subprocess

process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)  
process.stdin.write("dir\r\n")  
output = process.stdout.readlines()  
print output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感悟人生的甜 2024-08-24 08:44:52

通常,当尝试使用实际命令调用命令提示符时,使用“/k”参数调用它比通过标准输入传递命令更简单。也就是说,只需调用“cmd.exe /k dir”即可。例如,

from os import *
a = popen("cmd /k dir")
print (a.read())

下面的代码执行相同的操作,但缺少供您操作的字符串,因为它直接通过管道输出:

from subprocess import *
Popen("cmd /k dir")

Usually when trying to call command prompt with an actual command, it is simpler to just call it with the "/k" parameter rather than passing commands in via stdin. That is, just call "cmd.exe /k dir". For example,

from os import *
a = popen("cmd /k dir")
print (a.read())

The code below does the same thing, though lacks a string for you to manipulate, since it pipes to output directly:

from subprocess import *
Popen("cmd /k dir")
标点 2024-08-24 08:44:52
process = subprocess.Popen('cmd.exe /k ', shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
process.stdin.write("dir\n")
o,e=process.communicate()
print o
process.stdin.close()

顺便说一句,如果你的实际任务确实是做一个目录列表,请使用Python自己的os模块,例如os.listdir(),或glob模块...等。不必要时不要调用这样的系统命令。它使您的代码不可移植。

process = subprocess.Popen('cmd.exe /k ', shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
process.stdin.write("dir\n")
o,e=process.communicate()
print o
process.stdin.close()

by the way, if your actual task is really to do a directory listing, please use Python's own os module, eg os.listdir(), or glob module... etc. Don't call system commands like that unnecessarily. It makes your code not portable.

似最初 2024-08-24 08:44:52

这会锁定,因为 process.stdout.readlines() 读取进程的所有输出(直到它终止)。由于 cmd.exe 仍在运行,因此它会永远等待其关闭。

要解决此问题,您可以启动一个单独的线程来读取进程输出。如果您不调用 communicate(),则无论如何您都需要执行此操作,以避免可能的死锁。该线程可以重复调用process.stdout.readline()并处理数据或将其发送回主线程处理。

This locks up because process.stdout.readlines() reads all output by the process (until it terminates). Since cmd.exe is still running, it keeps waiting forever for it to close.

To fix this, you can start a separate thread to read the process output. This is something you need to do anyway if you don't call communicate(), to avoid possible deadlock. This thread can call process.stdout.readline() repeatedly and handle the data or send it back to the main thread for handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文