Run Linux commands in python

Source: Internet
Author: User

Example 1: Wrap the ls command in python #! /Usr/bin/env python # python wapper for the ls commandimport subprocesssubprocess. call (["ls", "-l"]) in Linux, run this command [root @ pydb python] # python ls. pytotal 8-rwxrwxrwx 1 root 415 Mar 18. py-rw-r -- 1 root 103 May 4 ls. under py, expand [root @ pydb python] # python ls. py #! /Usr/bin/env python # python wapper for the ls commandimport subprocesssubprocess. call (["cat", "/python/ls. in Linux. A very good example. Remember: The following example prints the system information and disk information: copy the Code #! /Usr/bin/env python # A System Information Gathering Scriptimport subprocess # command 1 uname = "uname" uname_arg = "-a" print "Gathering system information with % s command: \ n "% unamesubprocess. call ([uname, uname_arg]) # command 2 diskspace = "df" diskspace_arg = "-h" print "Gathering diskspace information % s command: \ n" % diskspacesubprocess. call ([diskspace, diskspace_arg]) Copy code. below is the printed data copy code that I run locally [root @ pydb python] # python info. pyGathering system information with uname command: Linux pydb 2.6.18-308. el5 #1 SMP Tue Feb 21 20:05:41 EST 2012 i686 i686 i386 GNU/LinuxGathering diskspace information df command: filesystem Size Used Avail Use % Mounted on/dev/mapper/VolGroup00-LogVol00 37G 3.9G 31G 11%/dev/sda1 99 M 13 M 82 M 13%/boottmpfs 506 M 0 506 M 0%/dev/shm [root @ pydb python] #

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.