os.system question

Carl Banks pavlovevidence at gmail.com
Fri Dec 28 15:06:33 EST 2007


On Dec 28, 1:52 pm, stanleyxu <no_re... at microsoft.com> wrote:
> Hi All,
>
> I am porting Perl script to Python script. Everything works fines until
> calling os.system().
>
> In my script, a number of DOS-commands will be executed.
>    for new_folder, old_folder in folder_array:
>        os.system('MD "' + new_folder + '"');
>        os.system('XCOPY "' + old_folder + '" "' + new_folder + '"');
>
> In Perl, all outputs will be printed in console directly.
> But in Python, outputs will be printed in separated cmd-windows.
>
> Is it possible to prevent so many cmd-windows to be opened and let all
> output be printed direct in Python shell?


Consider using the subprocess module instead.  It has more options
available than os.system, including I/O redirection, which seems to be
what you need.

In IDLE, you'll have to capture the output of the programs and print
it yourself, since you can't (AFAIK) run a DOS shell in an IDLE
window.  Untested:


import subprocess

output = subprocess.Popen('MD "' + new_folder + '"', shell=True,
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]
print output


Carl Banks



More information about the Python-list mailing list