Run the shell and pythonshell commands in python.

Source: Internet
Author: User
Tags glob

Run the shell and pythonshell commands in python.

Sh is a database better than subprocess and can execute shell commands

1. View ip Address:

[Root @ iZj6cbstl2n6r280a27eppZ myblog] # ifconfig
Eth0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
Inet 172.31.253.193 netmask 255.255.255.240.0 broadcast 172.31.255.255
Ether 00: 16: 3e: 01: 72: ac txqueuelen 1000 (Ethernet)
RX packets 235322 bytes 231762359 (221.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 78402 bytes 15382388 (14.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Complete with sh:

In [13]: from sh import ifconfig

In [14]: print ifconfig ("eth0 ")
Eth0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
Inet 172.31.253.193 netmask 255.255.255.240.0 broadcast 172.31.255.255
Ether 00: 16: 3e: 01: 72: ac txqueuelen 1000 (Ethernet)
RX packets 236463 bytes 231849592 (221.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 79234 bytes 15505613 (14.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2. view the files in the/app directory:

In [24]: print sh. ls ("/app ")
Git-2.14.1 myblog pyinit. py v2.14.1.tar.gz

In [27]: sh. ls ("-l", "/app ")
Out [27]:
Drwxrwxr-x 26 root 24576 9 ?. Git-2.14.1
Drwxr-xr-x 6 root 4096 9 ?. 13 myblog
-Rw-r -- 1 root 273 9 ?. 12 20:43 pyinit. py
-Rw-r -- 1 root 6983655 9 ?. 12 20:48 v2.14.1.tar.gz

View the number of files in the/app:

In [28]: sh. wc (sh. ls ("-l", "/app"), "-l ")
Out [28]: 5

3. Save the files in/app to the list:

In [42]: list = sh. glob ('/app /*')

In [43]: list
Out [43]: ['/app/git-2.14.1', '/app/v2.14.1.tar.gz', '/app/pyinit. py','/app/myblog ']

4. MPS queue:

In [45]: print (sh. sort (sh. du (sh. glob ('/app/*'), '-shc'),'-rn '))
397 M ?.?
378 M/app/git-2.14.1
13 M/app/myblog
6.7 M/app/v2.14.1.tar.gz
4.0 K/app/pyinit. py

 

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.