[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

os.system question

stanleyxu

12/28/2007 6:52:00 PM

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?


best regards ^^)
--
___
oo // \ (_,\/ \_/ \ Xu, Qian
\ \_/_\_/> stanleyxu2005
/_/ \_
7 Answers

stanleyxu

12/28/2007 6:58:00 PM

0

To note this problem occurs when debugging script in IDLE editor.
When I double click on my_script.py, all outputs will be printed in one
console.

--
___
oo // \ (_,\/ \_/ \ Xu, Qian
\ \_/_\_/> stanleyxu2005
/_/ \_

Mike Driscoll

12/28/2007 7:14:00 PM

0

On Dec 28, 12:57 pm, stanleyxu <no_re...@microsoft.com> wrote:
> To note this problem occurs when debugging script in IDLE editor.
> When I double click on my_script.py, all outputs will be printed in one
> console.
>
> --
> ___
> oo // \> (_,\/ \_/ \ Xu, Qian
> \ \_/_\_/> stanleyxu2005
> /_/ \_

Why are you using os.system for these commands in the first place? You
should be using the os and shutil modules instead as they would be
more cross-platform friendly.

Something like this:

# untested
for new_folder, old_folder in folder_array:
os.mkdir(new_folder)
shutil.copytree(old_folder, new_folder)


Adjust the path as needed in the mkdir call.

See shutil's docs for more info:
http://docs.python.org/lib/module-s...

And here's some folder manipulation docs:
http://effbot.org/libraryb...

By the by, the subprocess module is supposed to be used in place of
the os.system and os.popen* calls: http://docs.python.org/lib/module-subpr...

Mike

stanleyxu

12/28/2007 7:33:00 PM

0

kyosohma@gmail.com wrote:
> On Dec 28, 12:57 pm, stanleyxu <no_re...@microsoft.com> wrote:
>> To note this problem occurs when debugging script in IDLE editor.
>> When I double click on my_script.py, all outputs will be printed in one
>> console.
>>
>> --
>> ___
>> oo // \>> (_,\/ \_/ \ Xu, Qian
>> \ \_/_\_/> stanleyxu2005
>> /_/ \_>
>
> Why are you using os.system for these commands in the first place? You
> should be using the os and shutil modules instead as they would be
> more cross-platform friendly.
>
> Something like this:
>
> # untested
> for new_folder, old_folder in folder_array:
> os.mkdir(new_folder)
> shutil.copytree(old_folder, new_folder)
>
>
> Adjust the path as needed in the mkdir call.
>
> See shutil's docs for more info:
> http://docs.python.org/lib/module-s...
>
> And here's some folder manipulation docs:
> http://effbot.org/libraryb...
>
> By the by, the subprocess module is supposed to be used in place of
> the os.system and os.popen* calls: http://docs.python.org/lib/module-subpr...
>
> Mike

Thanks Mike,

you have provided another option.

But my question has not been answered yet. The reason, why I use
os.system(), is that I want to avoid accident file deletion by writing a
script. My real script looks like:

# 1. Funtion to execute a command in DOS-console
def execCommand(cmd):
if DEBUG_MODE:
print 'DOS> ' + cmd;
else:
os.system(cmd);

# 2.1 Creates temp folder. Removes it first, if it exists.
if os.path.exists(tmp_folder):
execCommand('RD "' + tmp_folder + '" /S /Q');
execCommand('MD "' + tmp_folder + '"');

# 2.2 Copies all files to the temp folder, that are going to be put in
package.
for source_folder, dest_folder in folders_array:
if not os.path.exists(dest_folder):
execCommand('MD "' + dest_folder + '"');
execCommand('XCOPY \"' + source_folder + '" "' + dest_folder + '" /Y');


The benefit is that, when I set DEBUG_MODE=True, I can see what will be
executed. So that I can make sure that my script will not delete any
other important files by accident.

--
___
oo // \ (_,\/ \_/ \ Xu, Qian
\ \_/_\_/> stanleyxu2005
/_/ \_

Carl Banks

12/28/2007 8:07:00 PM

0

On Dec 28, 1:52 pm, stanleyxu <no_re...@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

Mike Driscoll

12/28/2007 9:41:00 PM

0

On Dec 28, 1:32 pm, stanleyxu <no_re...@microsoft.com> wrote:
> kyoso...@gmail.com wrote:
> > On Dec 28, 12:57 pm, stanleyxu <no_re...@microsoft.com> wrote:
> >> To note this problem occurs when debugging script in IDLE editor.
> >> When I double click on my_script.py, all outputs will be printed in one
> >> console.
>
> >> --
> >> ___
> >> oo // \> >> (_,\/ \_/ \ Xu, Qian
> >> \ \_/_\_/> stanleyxu2005
> >> /_/ \_>
> > Why are you using os.system for these commands in the first place? You
> > should be using the os and shutil modules instead as they would be
> > more cross-platform friendly.
>
> > Something like this:
>
> > # untested
> > for new_folder, old_folder in folder_array:
> > os.mkdir(new_folder)
> > shutil.copytree(old_folder, new_folder)
>
> > Adjust the path as needed in the mkdir call.
>
> > See shutil's docs for more info:
> >http://docs.python.org/lib/module-s...
>
> > And here's some folder manipulation docs:
> >http://effbot.org/libraryb...
>
> > By the by, the subprocess module is supposed to be used in place of
> > the os.system and os.popen* calls:http://docs.python.org/lib/module-subpr...
>
> > Mike
>
> Thanks Mike,
>
> you have provided another option.
>
> But my question has not been answered yet. The reason, why I use
> os.system(), is that I want to avoid accident file deletion by writing a
> script. My real script looks like:
>


Technically speaking, the shutil module's copytree function will not
delete ANYTHING if the destination already exists. It will just fail.
You could catch the failed copy with a try/except that prints an
appropriate message detailing the error.

> # 1. Funtion to execute a command in DOS-console
> def execCommand(cmd):
> if DEBUG_MODE:
> print 'DOS> ' + cmd;
> else:
> os.system(cmd);
>
> # 2.1 Creates temp folder. Removes it first, if it exists.
> if os.path.exists(tmp_folder):
> execCommand('RD "' + tmp_folder + '" /S /Q');
> execCommand('MD "' + tmp_folder + '"');
>
> # 2.2 Copies all files to the temp folder, that are going to be put in
> package.
> for source_folder, dest_folder in folders_array:
> if not os.path.exists(dest_folder):
> execCommand('MD "' + dest_folder + '"');
> execCommand('XCOPY \"' + source_folder + '" "' + dest_folder + '" /Y');
>
> The benefit is that, when I set DEBUG_MODE=True, I can see what will be
> executed. So that I can make sure that my script will not delete any
> other important files by accident.
>
> --
> ___
> oo // \> (_,\/ \_/ \ Xu, Qian
> \ \_/_\_/> stanleyxu2005
> /_/ \_

Carl Banks mentioned the subprocess module too and he pointed out its
output redirection capabilities. I recommend checking those out too.
You may be able to do some redirection by changing where stdout and
stderr print to.

Mike

stanleyxu

12/28/2007 10:11:00 PM

0

Thanks again for your kindly tips.

--
___
oo // \ (_,\/ \_/ \ Xu, Qian
\ \_/_\_/> stanleyxu2005
/_/ \_

stanleyxu

12/28/2007 10:25:00 PM

0

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

Thanks Carl, it works ^^)

--
___
oo // \ (_,\/ \_/ \ Xu, Qian
\ \_/_\_/> stanleyxu2005
/_/ \_