天天看點

python 使用 sh 子產品執行 shell 指令

pip3 install sh

#!/usr/bin/python3

# -*- coding: UTF-8 -*-

import sh

print (sh.df("-h"))                                #df -h 指令

print (sh.echo("123456 abcde"))                    #echo 指令

print (sh.free("-m"))                              #free -m 指令

print (sh.fdisk("-l"))                             #fdisk -l 指令

sh.mkdir("-p","python_sh_test")                    #mkdir 指令

sh.touch("python_sh_test/py_test.txt")             #touch 指令

print (sh.cat("python_sh_test/py_test.txt"))       #cat 指令

print (sh.tail("-5","python_sh_test/py_test.txt")) #tail -n 指令

#print (sh.ls("-l"))                               #ls -l 指令

#print (sh.xargs(sh.ls("-l")))        #ls -l |xargs指令 (管道是有序的,預設由内而外)

#print (sh.grep(sh.ls("-l"),"txt$"))  #ls -l |grep txt$指令(管道是有序的,預設由内而外)