Call the system command in Python and get the return

Source: Internet
Author: User

Purpose: Sometimes you need some system information or execute files that are not Python, and you need to call them through the OS module.


Environment: Windows 7 Python 3.5.3


Scene:

1) I work with Python to build a file that the system can run directly, such as the bat batch, which needs to be executed;

2) I am familiar with batching and want to execute commands in Python and get its return information.


Both cases require the import of the OS module:

First case:

Os.open (file, flags[, mode]);

You can open the file in the system with the following parameters:

    • FILE--Files to open

    • Flags--This parameter can be the following options, separated by "|":

      • Os. O_rdonly: Open in read-only mode

      • Os. O_wronly: Open in write-only mode

      • Os. O_RDWR: Open in read-write mode

      • Os. O_nonblock: does not block when open

      • Os. O_append: Open in Append mode

      • Os. O_creat: Create and open a new file

      • Os. O_trunc: Open a file and truncate it to a length of 0 (Must have write permission)

      • Os. O_EXCL: If the specified file exists, an error is returned

      • Os. O_shlock: Automatic acquisition of shared locks

      • Os. O_exlock: Automatic acquisition of independent locks

      • Os. O_direct: Eliminate or reduce cache effects

      • Os. O_fsync: Synchronous Write

      • Os. O_nofollow: Do not track soft links

    • Mode-similar to chmod (), this parameter is commonly used in Linux, the default 0777

return value

Returns the descriptor for the newly opened file.

Here are two examples:

1) Operation text file similar to open ()

With Os.open (' Test.txt ', OS. O_rdwr|os. O_creat) as F:os.write (F, ' This is a test. ')

Well, that's it, using it to feel more trouble than open (link).


2) Execute a batch file that already exists

Os.open (' Test.bat ')

Well, it's a simple sentence.


Second case:

Os.popen (command[, mode[, BufSize])

I want to get the current user name:

echo%username% in Windows can output the current user name

user = Os.popen (' echo%username% '). Read ()

Well, that's it, it feels good to use the combination.


This article is from the "Rickyhul" blog, make sure to keep this source http://rickyh.blog.51cto.com/10934856/1947783

Call the system command in Python and get the return

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.