成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

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

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

調(diào)用shell命令

#!/usr/bin/python3

import subprocess

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

# 傳遞選項(xiàng)和參數(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)庫(kù)的一部分

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

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

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

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

Today is Tuesday

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

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

#!/usr/bin/python3

import subprocess

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

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

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

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

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

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

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

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

$ ./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")

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

# 像這樣使用另一種引號(hào)
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ù)字符串太常,我們使用變量先存儲(chǔ)命令
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

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

# 獲取命令執(zhí)行的狀態(tài)和輸出
# 退出狀態(tài)碼不是“0”意味著有什么事情出錯(cuò)了
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))

# 抑制出錯(cuò)信息
# 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ù)

使用有更多特性和安全選項(xiàng)的新函數(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)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

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

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

廣告

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

小程序開發(fā)
临猗县| 蒲城县| 彭阳县| 宜昌市| 托克逊县| 甘南县| 天峨县| 呼伦贝尔市| 河北省| 门头沟区| 会同县| 罗定市| 西和县| 冀州市| 杭锦旗| 和硕县| 扶风县| 曲阜市| 阳春市| 锡林浩特市| 聊城市| 河南省| 微博| 锡林浩特市| 曲麻莱县| 图片| 江陵县| 昌江| 嵊州市| 堆龙德庆县| 台南县| 锡林郭勒盟| 定日县| 枣强县| 东宁县| 大余县| 泸州市| 县级市| 克拉玛依市| 黄山市| 浦江县|