Win XP: Problem with shell scripting in Python

A.M alanalan at newsgroup.nospam
Fri Jun 9 00:58:24 CEST 2006


Hi,



I am having difficulty with shell scripting in Python.



I use the following command to run a DOS command and put the return value in 
a Python variable:



print os.popen('DIR').read()



It works very fine with DIR command, but for commands like "MD :" it doesn't 
return the error message into the string:



print os.popen('MD :').read()

# No error message



When I use Ruby, it works perfect:



`md :`

The filename, directory name, or volume label syntax is incorrect.



I am also having problem with redirecting the python script output to a 
file: That means I can redirect the output to a file by using pipes like 
this:



Python.exe script.py >file.txt



But the sequence of contents in file.txt  doesn't match with command 
execution sequence!

When I don't use pipes, the output sequence is fine when I see the output on 
the monitor screen.



Am I missing anything? Considering the fact that Ruby doesn't have any 
problem with redirecting STDOUT into files or string variables, is Python 
the right tool for this kinds of shell scripting?



Any help would be appreciated,

Alan







More information about the Python-list mailing list