Mega Code Archive

 
Categories / Python Tutorial / Buildin Module
 

Demonstrating popen and popen2

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()