0

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 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?

2
  • Is there a reason you're using subprocess rather than the standard Python tools for creating files? Commented Mar 10, 2011 at 4:45
  • 1
    yes, of course: I'm actually using the mklink command to create symlinks to files. I just tried to reduce the problem to a smaller, more general command which is easier to write and doesn't need any prerequisites. The problem is identical in both commands. Commented Mar 10, 2011 at 5:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

16 Comments

No, I'm not ... but if I just run the commands one by one by hand, it doesn't generate any output either. The files just don't appear. The problem with reading from the pipes is that it interrupts the program if I want to read more characters than have been written. I don't get a '' at the end of file, read(1) just blocks ...
I just tried peek() and it works, but I only see that there is absolutely no output ... which hinted me at a possible solution: I might try flush on the input stream ...
Try using cmdprocess = subprocess.Popen("cmd", stdin = subprocess.PIPE, shell = True)
@Bo: I tried that, it now says cmdprocess.stdin.write(command) IOError: [Errno 22] Invalid argument before it stops. Any idea what's going wrong there? Does this mean that stdin as the implicit first argument to write somehow suddenly ceased to exist? I think I'm trying the debugger on this ...
From what I have it runs all the way through 499 I just get access denied errors before it won't let me write files directly at c:\ here is the full code I have import subprocess import os cmdprocess = subprocess.Popen("cmd", stdin = subprocess.PIPE, shell = True) for i in range(500): command = ("dir > " + os.path.join("C:\\", str(i)) + "\r\n").encode("utf-8") print(command) cmdprocess.stdin.write(command)
|
1

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.

1 Comment

it's quite long... plz.check flankton.tistory.com/entry/Python-Command-Process out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.