os.system function

Z

Zabin

Hey everyone!

I am a new python programmer. I am trying to get the general file
functionality with options of save and save as working. These save
functions save a folder with multiple files. Upon using the os.system
copy function- if my destination directory has files with similar
names- i am asked whether i want to replace the files on the command
prompt.

Is there some way of getting this question into a dialog box?

Also is there someway of avoiding or programmatically setting the
response to the command prompt?

Cheers
Zabin
 
O

Olof Bjarnason

2010/1/11 Zabin said:
Hey everyone!

I am a new python programmer. I am trying to get the general file
functionality with options of save and save as working. These save
functions save a folder with multiple files. Upon using the os.system
copy function- if my destination directory has files with similar
names- i am asked whether i want to replace the files on the command
prompt.

Is there some way of getting this question into a dialog box?

Also is there someway of avoiding or programmatically setting the
response to the command prompt?

I recommend the shutil module, which is built-into Python:

http://docs.python.org/library/shutil.html

You will have more control of what you are doing with it.
 
C

Chris Rebert

Hey everyone!

I am a new python programmer. I am trying to get the general file
functionality with options of save and save as working. These save
functions save a folder with multiple files. Upon using the os.system
copy function- if my destination directory has files with similar
names- i am asked whether i want to replace the files on the command
prompt.

Is there some way of getting this question into a dialog box?

Also is there someway of avoiding or programmatically setting the
response to the command prompt?

You can probably avoid using os.system() entirely (always a good
thing) by instead using the functions in the `shutil` library:
http://docs.python.org/library/shutil.html

And for future reference, the `subprocess` module is the preferred way
to invoke external programs, rather than os.system():
http://docs.python.org/library/subprocess.html

Cheers,
Chris
 
Z

Zabin

You can probably avoid using os.system() entirely (always a good
thing) by instead using the functions in the `shutil` library:http://docs..python.org/library/shutil.html

And for future reference, the `subprocess` module is the preferred way
to invoke external programs, rather than os.system():http://docs.python.org/library/subprocess.html

Cheers,
Chris
--http://blog.rebertia.com

Thanks for the pointers....i had a look around and found the site:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

to disable the prompt- i needed to include /y as below:
os.system ('xcopy /s %s %s /y ' % (dirExe, dirname_new))


and just wondering- whats the drawback of using os.system() command

Appreciate the prompt suggestions!

Cheers
Zabin
 
J

Jeremy Sanders

Zabin said:
Thanks for the pointers....i had a look around and found the site:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-
us/xcopy.mspx?mfr=true

to disable the prompt- i needed to include /y as below:
os.system ('xcopy /s %s %s /y ' % (dirExe, dirname_new))


and just wondering- whats the drawback of using os.system() command

- It won't work across different platforms (unix, mac, windows)
- Spaces or special characters in the filename will mess up the command line
and can lead to huge security flaws in your program.
- It's inefficient as you have to start a new program to do the work (slow
on windows)
- Error handling from the xcopy process will not be easy
 
C

Chris Rebert

and just wondering- whats the drawback of using os.system() command

Forgetting to properly escape your input. Simple example:

filename = "foo bar.txt"
os.system("rm "+filename) # uh-oh, we deleted 'foo' and 'bar.txt' instead

The `subprocess` module makes escaping unnecessary.

Cheers,
Chris
 
Z

Zabin

- It won't work across different platforms (unix, mac, windows)
- Spaces or special characters in the filename will mess up the command line
and can lead to huge security flaws in your program.
- It's inefficient as you have to start a new program to do the work (slow
on windows)
- Error handling from the xcopy process will not be easy

wow- thanks heaps!
 
R

r0g

Zabin said:
Hey everyone!

I am a new python programmer. I am trying to get the general file
functionality with options of save and save as working. These save
functions save a folder with multiple files. Upon using the os.system
copy function- if my destination directory has files with similar
names- i am asked whether i want to replace the files on the command
prompt.

Is there some way of getting this question into a dialog box?

You'll need to use one of the GUI toolkits for that.

wxPython is my prefered one as its cross platform and it renders with
the OS's native toolkit / look. However, that is quite a big dependency
(10MiB) to be carrying around if all you need is a dialog box so you may
want to look into pythons core GUI library, TKL.

Roger.
 
R

r0g

Lie said:
I know Tk and Tcl has been close since their childhood; did they get
married too?

Whoops... yes Tk/Tcl, it seems they had become one in my head only!

:)

Roger.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top