开发者

What's the problem with executing commands in Windows CMD from Python?

I'm having huge trouble with passing commands to CMD from Python.

First, I open a CMD process:

    cmdprocess = subprocess.Popen("cmd",
                                  stdin  = subprocess.PIPE,
                                  stdout = subprocess.PIPE,
                                  stderr = subprocess.PIPE)

Then, I do something, for example:

for i in range(500):
     #time.sleep(1)
     command = ("dir > " + os.path.join("C:\\", str(i)) + "\r\n").encode("utf-8")
     print(command)
     cmdprocess.stdin.write(command)

So this is supposed to create 500 small text files in a folder. I tested it in Python 3.2 x64 and 3.2 x86 and the result for both is: it counts up to about 250-350 in the Python shell, and then just stops. No error, nothing. There开发者_StackOverflow中文版 are then the files 1-80 in the specified folder.

Now, I thought that maybe Python is too fast and so had it sleep(1) for 1 second between the commands. Now, it counts up to about 200 before the first file appears in the folder! and then stops at about 270.

What happens here and how can I force CMD to execute a command immediately?


Are you handling the output in the PIPES? They might be filling. If you fill up the stdout or stderror buffers from the process, it will stop execution.


I think you'd better to use pywin32 package. there are win32pipe and win32process modules. I also had same issue but I could not resolve it without pywin32-site-package... So now I am using them... If you need sample code and you're using windows, I will attach it. if you mean linux... it's same but you need another one like IO select.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜