欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python執(zhí)行外部命令的方法

這篇文章給大家分享的是有關(guān)Python執(zhí)行外部命令的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

威遠網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)建站2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站

調(diào)用shell命令

#!/usr/bin/python3

import subprocess

# 執(zhí)行外部命令 'date'
subprocess.call('date')

# 傳遞選項和參數(shù)給命令
print("\nToday is ", end="", flush=True)
subprocess.call(['date', '-u', '+%A'])

# 其他例子
print("\nSearching for 'hello world'", flush=True)
subprocess.call(['grep', '-i', 'hello world', 'hello_world.py'])

這里的import語句用于載入subprocess模塊,它是Python標(biāo)準(zhǔn)庫的一部分

subprocess模塊中的call函數(shù)是一種執(zhí)行外部命令的方式

通過傳遞True給flush參數(shù)(默認(rèn)是False),我們確保這個信息在subprocess.call運行之前輸出

想要給命令傳遞參數(shù),需要使用字符串列表

$ ./calling_shell_commands.py
Tue Jun 21 18:35:33 IST 2016

Today is Tuesday

Searching for 'hello world'
print("Hello World")

使用擴展調(diào)用shell命令

#!/usr/bin/python3

import subprocess

# 不使用擴展調(diào)用Shell命令
print("No shell expansion when shell=False", flush=True)
subprocess.call(['echo', 'Hello $USER'])

# 使用Shell擴展調(diào)用Shell命令
print("\nshell expansion when shell=True", flush=True)
subprocess.call('echo Hello $USER', shell=True)

# 如果引號是命令的一部分則進行反義
print("\nSearching for 'hello world'", flush=True)
subprocess.call('grep -i \'hello world\' hello_world.py', shell=True)

默認(rèn)subprocess.call不會擴展shell 通配符,使用 command替換等等

可以設(shè)定shell參數(shù)為True進行重寫

注意現(xiàn)在整個命令行都作為一個字符串而不是字符串列表

命令中含有引號如要轉(zhuǎn)義

僅在你確定命令會正確執(zhí)行的情況下使用shell=True,否則會存在安全問題

$ ./shell_expansion.py
No shell expansion when shell=False
Hello $USER

shell expansion when shell=True
Hello learnbyexample

Searching for 'hello world'
print("Hello World")

在特定情況下,我們可以使用單/雙引號的組合來避免使用轉(zhuǎn)義符號

# 像這樣使用另一種引號
subprocess.call('grep -i "hello world" hello_world.py', shell=True)

# 或者這樣
subprocess.call("grep -i 'hello world' hello_world.py", shell=True)

Shell命令重定向可以正常使用

# 為了避免call函數(shù)字符串太常,我們使用變量先存儲命令
cmd = "grep -h 'test' report.log test_list.txt > grep_test.txt"
subprocess.call(cmd, shell=True)

避開使用shell=True

>>> import subprocess, os
>>> subprocess.call(['echo', 'Hello', os.environ.get("USER")])
Hello learnbyexample
0

os.environ.get("USER")返回環(huán)境變量USER的值

0退出狀態(tài)碼,意味著成功執(zhí)行了命令。

獲取命令輸出和重定向

#!/usr/bin/python3

import subprocess

# 輸出也包含任何的錯誤信息
print("Getting output of 'pwd' command", flush=True)
curr_working_dir = subprocess.getoutput('pwd')
print(curr_working_dir)

# 獲取命令執(zhí)行的狀態(tài)和輸出
# 退出狀態(tài)碼不是“0”意味著有什么事情出錯了
ls_command = 'ls hello_world.py xyz.py'
print("\nCalling command '{}'".format(ls_command), flush=True)
(ls_status, ls_output) = subprocess.getstatusoutput(ls_command)
print("status: {}\noutput: '{}'".format(ls_status, ls_output))

# 抑制出錯信息
# subprocess.call()返回命令的狀態(tài)碼 returns status of command which can be used instead
print("\nCalling command with error msg suppressed", flush=True)
ls_status = subprocess.call(ls_command, shell=True, stderr=subprocess.DEVNULL)
print("status: {}".format(ls_status))

getstatusoutput()的輸出是元組數(shù)據(jù)類型,更多信息和例子在后續(xù)章節(jié)

getstatusoutput()和getoutput()老式的函數(shù)

使用有更多特性和安全選項的新函數(shù)

$ ./shell_command_output_redirections.py
Getting output of 'pwd' command
/home/learnbyexample/Python/python_programs

Calling command 'ls hello_world.py xyz.py'
status: 2
output: 'ls: cannot access xyz.py: No such file or directory
hello_world.py'

Calling command with error msg suppressed
hello_world.py
status: 2

感謝各位的閱讀!關(guān)于Python執(zhí)行外部命令的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

本文名稱:Python執(zhí)行外部命令的方法
當(dāng)前網(wǎng)址:http://chinadenli.net/article18/ppeedp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化網(wǎng)站收錄網(wǎng)站制作小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)