Scripting >> Python >> Examples >> Modules >> os >> How to use os module

 

Run operating system command from python Windows example

>>> import os
>>> os.system('dir')
 Volume in drive C is Windows8_OS
 Volume Serial Number is BE98-550F

 Directory of C:\Program Files\Microsoft Office\Office12

15/08/2016  09:57 PM    <DIR>          .
15/08/2016  09:57 PM    <DIR>          ..
05/01/2016  06:31 PM    <DIR>          1033
13/08/2006  04:51 PM                78 Mso Example Setup File A.txt
26/10/2006  07:28 PM           110,392 MSOHEVI.DLL
26/10/2006  08:03 PM           546,104 ONFILTER.DLL
26/10/2006  08:25 PM           483,632 VISSHE.DLL
               4 File(s)      1,140,206 bytes
               3 Dir(s)  64,930,074,624 bytes free
0

Unix example

>>> import os
>>> os.system('ls -l /tmp')
total 4
drwxr-xr-x 2 root root 4096 Sep 19 22:25 hsperfdata_root
0
Windows example

>>> import subprocess
>>> subprocess.call('dir c:\temp',shell=True)
 Volume in drive C is Windows8_OS
 Volume Serial Number is BE98-550F

 Directory of C:\Program Files\Microsoft Office\Office12

15/08/2016  09:57 PM    <DIR>          .
15/08/2016  09:57 PM    <DIR>          ..
05/01/2016  06:31 PM    <DIR>          1033
13/08/2006  04:51 PM                78 Mso Example Setup File A.txt
26/10/2006  07:28 PM           110,392 MSOHEVI.DLL
26/10/2006  08:03 PM           546,104 ONFILTER.DLL
26/10/2006  08:25 PM           483,632 VISSHE.DLL

 Directory of C:\Program Files\Microsoft Office\Office12

               4 File(s)      1,140,206 bytes
               3 Dir(s)  64,929,660,928 bytes free
0


Unix example

>>> import subprocess
>>> subprocess.call('ls -l /tmp',shell=True)
total 4
drwxr-xr-x 2 root root 4096 Sep 19 22:25 hsperfdata_root
0