os.system question
stanleyxu
no_reply at microsoft.com
Fri Dec 28 14:32:44 EST 2007
kyosohma at gmail.com wrote:
> On Dec 28, 12:57 pm, stanleyxu <no_re... at 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-shutil.html
>
> And here's some folder manipulation docs:
> http://effbot.org/librarybook/os.htm
>
> 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-subprocess.html
>
> 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
/_/ \_\
More information about the Python-list
mailing list