The difference between Python Os.system and Os.popen

Source: Internet
Author: User

(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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.