473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.system question

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('XCOP Y "' + 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
/_/ \_\
Dec 28 '07 #1
6 2836
On Dec 28, 12:57 pm, stanleyxu <no_re...@micro soft.comwrote:
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_fo lder)
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
Dec 28 '07 #2
ky******@gmail. com wrote:
On Dec 28, 12:57 pm, stanleyxu <no_re...@micro soft.comwrote:
>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_fo lder)
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('XC OPY \"' + 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
/_/ \_\
Dec 28 '07 #3
On Dec 28, 1:52 pm, stanleyxu <no_re...@micro soft.comwrote:
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('XCOP Y "' + 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.Pope n('MD "' + new_folder + '"', shell=True,
stdout=subproce ss.PIPE, stderr=subproce ss.STDOUT).comm unicate()[0]
print output
Carl Banks
Dec 28 '07 #4
On Dec 28, 1:32 pm, stanleyxu <no_re...@micro soft.comwrote:
kyoso...@gmail. com wrote:
On Dec 28, 12:57 pm, stanleyxu <no_re...@micro soft.comwrote:
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_fo lder)
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:

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('XC OPY \"' + 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
Dec 28 '07 #5
Thanks again for your kindly tips.

--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_\
Dec 28 '07 #6
>
import subprocess

output = subprocess.Pope n('MD "' + new_folder + '"', shell=True,
stdout=subproce ss.PIPE, stderr=subproce ss.STDOUT).comm unicate()[0]
print output
Carl Banks
Thanks Carl, it works ^^)

--
___
oo // \\
(_,\/ \_/ \ Xu, Qian
\ \_/_\_/ stanleyxu2005
/_/ \_\
Dec 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12050
by: Abraham Lopez | last post by:
Hi.. Is there a way to convert a System.Array to XML... If you know thanks very much... if you don't... Please do not respond stupid things like " Yes -- many ways."
7
7540
by: MLH | last post by:
Where is system.mdw normally stored in typical A97 installation?
20
13138
by: SR | last post by:
Hi, I need to read the output from a system( ) function within a C program. The function however only returns the exit status. How can I read what system( ) sends to stdout ? (is there a simpler way than having to fork a child process and then catching its output ?) (if needed: working on linux) Thanks
4
7190
by: The Spoon | last post by:
I am looking for functions that can be used to automatically update the system date and time on an NT based system, resulting from manually entered date and time values input by an operator via a 32-bit console app. Any ideas?..I may have to look further afield at Win32 API's, etc but I wondered if there was any way to do it via C initially. Merry Xmas to all !!!
5
3547
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and function calls) are part of the (Unix) system calls. The cause of my confusion is that for example stdio.h is considered
8
7855
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's in the namespace "System.Text.RegularExpressions;" and that namespace is, in turn, defined in the namespace "System", according to MSDN at http://msdn2.microsoft.com/en-us/library/c75he57e(en-us,VS.80).aspx. So adding "using...
1
4889
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day or so ago everything was working fine. I honestly can't remember changing anything since it was last working. But I tried reinstalling the .Net Framework along w/ the service pack, which didn't work. I also had v1.1 already installed but I hadn't...
1
2157
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems down the line... What happens when my code is run on a station that only has framework 3.0 or 4.0, and this assembly, with version number defined for 2.0.0.0 , isn't available. ...breaks? Second question: Does an assembly's PublicKeyToken change...
10
3366
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.