python 調用linux shell

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import subprocess
import time

'''
a =os.popen('sh','w+')
p.write("ls -l")
p.read()


Time_Now=time.strftime('%Y%m%d',time.localtime(time.time()))

cmd2='script -a /home/neal/Documents/Log_local_terminal/terminal'+Time_Now+'.txt'
print '#',cmd2
p = subprocess.Popen(cmd2,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
'''
subprocess.Popen('ride.py &',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

'''

cmd1='cd / ;ls -l'
p = subprocess.Popen('cd / ;ls -l',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
print '#',cmd1
for line in p.stdout.readlines():
    print line,
retval = p.wait()

cmd='ping -c 4 baidu.com'
w = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
print '#',cmd
for line in w.stdout.readlines():
    print line,
retval = w.wait()

'''

常用的幾種shell命令調用方式:

#!/usr/bin/env bash 

import os
import commands
import subprocess

# ①-- os.system() --
# execute shell command in sub-terminal and can not get the return result
result1 = os.system('ls -l .')
print('result: ')
print(result1)
print('----------------------------------------')

# ②-- os.popen() --
# execute and can get the return result
result2 = os.popen('ls -l')
# file type
print('type: ', type(result2))

result3 = os.popen('ls -l').readlines()
# list file
print('type: ', type(result3))
print('result: ')
print(result3)
print('----------------------------------------')

#③ -- commands --
# import commands
# method:
#    getoutput
#    getstatusoutput
result4 = commands.getoutput('ls -l')
print('result: ')
print(result4)
print('=======')
result5_status, result5_output = commands.getstatusoutput('ls -l')
print('status: ', result5_status)
print('output: ')
print(result5_output)
print('----------------------------------------')



# ④subprocess
# import subprocess
# method:
#    call(["cmd","arg1", "arg2"], shell=True) refer to os.system()
#   Popen("cmd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) refer to os.popen
#
#result6 = subprocess.call("ls -l", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p = subprocess.Popen('ls *.txt', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.stdout.readlines())

for line in p.stdout.readlines():
    print line,

## wait for child process to terminate, and turn returncode
retval = p.wait()
##  if ok, return 0
print(retval)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,185評論 2 33
  • Shell簡介 Shell會執(zhí)行用戶輸入的命令,并且在屏幕上顯示執(zhí)行的結果。 單從字面的意思上理解,Shell的本...
    故事狗閱讀 2,200評論 2 10
  • 為何叫做 shell ? shell prompt(PS1) 與 Carriage Return(CR) 的關系?...
    Zero___閱讀 3,314評論 3 49
  • 這周帶兩條狗狗過來了,每天就帶著狗狗散步。上班就關在屋里還是多可憐的不是嗎。早晨去走一走,晚上再走一走,狗狗也高興...
    曉宇如風閱讀 226評論 0 0
  • 為貫徹落實董事長“抓好基礎建設,提升管理水平”的工作要求,打造學習型組織,2017年4月10日下午3:30-5:...
    d6d961cb0bd3閱讀 352評論 0 0

友情鏈接更多精彩內容