Python learning experience

Source: Internet
Author: User

Python popenIt is a method in the python OS module. It is very good to execute some system commands, and it can return something you want.
The specific explanation of the python popen is to open a command or a slave pipeline. The returned value is an open file object connected to the pipeline. The read/write mode is based on whether it is 'R' (preset) or 'W '. The bufsize argument is the same as the corresponding parameter Meanings of the built-in open () function. This command (waiting for the exit status of encoding in the specified format () is considered as the return value of the close () file object method, it is not returned unless the exit status is zero (no error is terminated. Availability: Unix operating system and windows.

Suppose we want to write information about all the files in the current path in Linux:
For I in OS. popen ('LS-al '):
Print I

The returned result is:
-RW ------- 1 WL wheel 73981952 1 11 2010 python. Core

-RW ------- 1 WL wheelel 69844992 12 12 2009 python2.5.core

Drwxr-XR-x 7 WL wheel 1024 11 27 2009 Release

-RW-r -- 1 WL wheel 17 1 8 2010 Renren. coockie

-RW-r -- 1 WL wheel 24936 11 1 2008 scrumper. py
...

Based on the above results, you can use OS. popen is used to execute some other system methods. My current working platform is on Unix. You can also see how to use Python popen on Windows to execute some system commands, I believe we can have different surprises.

OriginalArticlePlease indicate Reprinted from Old Wang python, this article address: http://www.cnpythoner.com/post/87.html

Author: Lao Wang @ Python tutorial
Old Wang Python provides Python-related Django tutorials and Python downloads!

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.