Demonstrating popen and popen2. : os « Buildin Module « Python Tutorial






import os

if os.name == "nt" or os.name == "dos":  
   fileList = "dir /B"
   sortReverse = "sort /R"
elif os.name == "posix":  # UNIX-compatible system
   fileList = "ls -1"
   sortReverse = "sort -r"
else:
   sys.exit( "OS not supported by this program." )

dirOut = os.popen( fileList, "r" )
sortIn, sortOut = os.popen2( sortReverse )

filenames = dirOut.read()  

print "(Output from '%s'):" % fileList
print filenames

sortIn.write( filenames )  

dirOut.close()
sortIn.close()

print "(Output from '%s'):" % sortReverse
print sortOut.read() 

sortOut.close()








14.9.os
14.9.1.Starting a Web browser.
14.9.2.Windows-specific function os.startfile:
14.9.3.Using the os.execvp(path, args) to execute the application update.exe with a command-line parameter of -verbose
14.9.4.list directories
14.9.5.Execute system command with os.system()
14.9.6.Here is an example executing a DOS command
14.9.7.os.popen() a pipe
14.9.8.Operating System Interface
14.9.9.Process Management
14.9.10.Run application from host
14.9.11.Run shell command
14.9.12.fork a thread
14.9.13.Uses the system function to clear the screen.
14.9.14.Opens a Web page in a system-specific editor
14.9.15.Demonstrating popen and popen2.