faqts : Computers : Programming : Languages : Python

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 22 people (64%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

how do I pass arguments (whats the correct syntax) to the os.system command?

Sep 5th, 2002 09:03
Michael Chermside, Jason Welsh,


It is quite simple:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('del delme')
0

This was on Windows, but the usage on a unix variant is exactly the
same. Simply put exactly what you would type at the command prompt into
a string, and pass it to os.system().

Of course, it's worth mentioning that if you want to see the output,
you'll have to use some variant of popen() instead... something like this:

>>> print os.popen('ls -n').read()
total 611295
drwxrwxrwx   1 1        1              0 Sep  3 14:47 Applications
drwxrwxrwx   1 1        1              0 Apr  2  2001 DRIVERS
drwxrwxrwx   1 1        2              0 Sep  3 16:09 Documents
drwxrwxrwx   1 1        1              0 Nov  5  2001 FOUND.000
drwxrwxrwx   1 1        2              0 Jul 16 13:11 NetAccounts
drwxrwxrwx   1 1        1              0 Jul 30 15:44 Program Files
drwxrwxrwx   1 1        1              0 Apr 19 07:41 Reference
drwxrwxrwx   1 1        1              0 Sep  3 14:49 TEMP
drwxrwxrwx   1 1        1              0 Sep  3 15:44 WINNT
drwxrwxrwx   1 1        1              0 Sep  4  2001 bea
-rwxrwxrwa   1 1        1             43 Apr  2  2001 cmdlines.$$$
drwxrwxrwx   1 1        2              0 Feb 19  2002 cygwin
-rwxrwxrwa   1 1        1            608 Feb 14  2002 sh_histo