Python calls external command: OS module

Source: Internet
Author: User
Tags stdin

Note: The following uses the OS module to invoke external commands, but the module is deprecated, and usually we use the Subpross module

Os.system (): Result of command output on terminal, not captured

In [1]: import osin [2]: Os.system ('ls/tmp/')12 . txt 1. txt 2. txt        3.  test1  test2    # Here's the result output to the terminal out[20                                      # This returns the status code after the command is executed 

Os.popen (): Returns 1 objects, standard output

in [4 out = Os.popen ('ls/tmp/')    # receives standard output by variable in [5out. Read ()    # can be viewed using read (), ReadLine (), ReadLines (), and so on out[  5'12.txt\n1.txt\n2.txt\n3.txt\ntest1\ntest2\n'

Os.popen2 (): Returns 2 objects, standard input, standard output, respectively

In [3]: stdin, stdout = Os.popen2 ('ls/tmp/' # We use the stdin to receive the standard input and use the STDOUT to receive the standard output in [4]: Stdout.read () # The standard output can be viewed through the Read () method out[4]:'12.txt\n1.txt\n2.txt\n3.txt\ntest1\ntest2\n'In [5]: Stdin.write ('Hello' #) can also be written to standard input in [using the Write () method. 6]: Stdin.close ()# Close () to save the write content

Os.popen3 (): Returns 3 objects, standard input, standard output, standard error output, respectively

In [7]: stdin, stdout, stderr = Os.popen3 ('ls/tmp/aaa')in[  9]: Stderr.read ()    # View standard error output out[9'ls:cannot access/tmp/aaa: No such file or directory\n'

Python calls external command: OS module

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.