(1) Os.system
# Run System commands on only one sub-terminal and cannot get return information after command execution
System (command), Exit_status
Execute the command (a string) in a subshell.
# if the command line executes, the result is printed directly.
1 >>> os.system ('ls')2304101419778 . CHM Bash document media py-Django video45 . wmv Books downloads Pictures python67 all-20061022 Desktop Examples Project Tools
(2) Os.popen
# This method not only executes the command but also returns the executed information object
Popen (command [, mode= ' R ' [, BufSize]]), pipe
Open a pipe to/from a command returning a file object.
For example:
1>>>tmp = Os.popen ('ls *.py'). ReadLines ()2 3>>>tmp4 5 out[]:6 7['dump_db_pickle.py',8 9 'dump_db_pickle_recs.py',Ten One 'dump_db_shelve.py', A - 'initdata.py', - the '__init__.py', - - 'make_db_pickle.py', - + 'make_db_pickle_recs.py', - + 'make_db_shelve.py', A at 'peopleinteract_query.py', - - 'reader.py', - - 'testargv.py', - in 'teststreams.py', - to 'update_db_pickle.py', + - 'writer.py']
The advantage is that the returned result is assigned to a variable to facilitate the processing of the program.
The difference between Python Os.system and Os.popen