Python系統命令操作

2022-01-19 02:37:25 字數 3419 閱讀 2924

1、call

執行命令,返回狀態碼

ret = subprocess.call(['

ls', '

-l'], shell=false)

ret = subprocess.call('

ls -l

', shell=true)

2、check_call

執行命令,如果執行狀態碼是0,則返回0,否則拋異常

subprocess.check_call(["

ls", "-l"

])

subprocess.check_call(

"exit 1

", shell=true)

3、check_output

執行命令,如果狀態是0,則返回執行結果,否則拋異常

subprocess.check_output(["

echo

", "

hello world !

"])

subprocess.check_output([

"exit 1

", shell=true])

用於執行複雜的系統命令

引數:

所以不能將close_fds設定為true同時重定向子程序的標準輸入、輸出與錯誤(stdin,stdout,stderr)

案例:

import

subprocess

ret1 = subprocess.popen(["

mkdir

", "t1"

])ret2 = subprocess.popen("

mkdir t2

", shell=true)

輸入即可得到輸出,如:ifconfig

輸入進行某環境,依賴在輸入,如:python

import

subprocess

obj = subprocess.popen("

mkdir t3

", shell=true, cwd='

/home/dev

')

import

subprocess

obj = subprocess.popen(["

python

"], stdin=dubprocess.pipe,stdout=subprocess.pipe,stderr=subprocess.pipe, universal_newlines=true)

obj.stdin.write(

"print(1)\n

")

obj.stdin.write(

"print(2)

")

obj.stdin.close()

cmd_out =obj.stdout.read()

obj.stdout.close()

cmd_error =obj.stderr.read()

obj.stderr.close()

print

(cmd_out)

print(cmd_error)

view code

import

subprocess

obj = subprocess.popen(["

python

"], stdin=subprocess.pipe, stdout=subprocess.pipe, stderr=subprocess.pipe, universal_newlines=true)

obj.stdin.write(

"print(1)\n

")

obj.stdin.write(

"print(2)

")

out_error_list =obj.communicate()

print(out_error_list)

view code

import

subprocess

obj = subprocess.popen(["

python

"], stdin=subprocess.pipe, stdout=subprocess.pipe, stderr=subprocess.pipe, universal_newlines=true)

out_error_list = obj.communicate('

print("hello")

')

print(out_error_list)

view code

import os

import subprocess

obj = subprocess.popen("

ifconfig

", shell=true, stdout =subprocess.pipe)

print(

"***************====")

print(obj.stdout.read().decode(

"utf-8"))

print(

"***************====

")

看自己需求,可以進行修改

import

subprocess

def local_exec_shell_command(shell_command, is_shell=true):

"""@:param shell_comamnd

@:return exec_result(true or false) _stdout _stderr

"""try

: process =subprocess.popen(

shell_command,

shell=is_shell,

stdout=subprocess.pipe,

stderr=subprocess.pipe

)_stdout, _stderr =process.communicate()

if process.returncode !=0:

return

false, _stdout, _stderr

return

true, _stdout, _stderr

except

exception as e:

return false, ''

, str(e)

if__name__ == '

__main__':

data = local_exec_shell_command("

ifconfig")

print(data)

系統命令操作

系統命令 1 call 執行命令,返回狀態碼 ret subprocess.call ls l shell false ret subprocess.call ls l shell true 2 check call 執行命令,如果執行狀態碼是0,則返回0,否則拋異常 subprocess.chec...

系統操作命令

netstat ano findstr 443 意思是查詢 443 埠的程序 taskkill f pid 6076 意思是殺程序 window下命令啟動 停止nginx 檢視nginx的版本號 nginx v 啟動nginx start nginx 快速停止或關閉nginx nginx s sto...

linux系統操作命令

對系統操作 關機 shutdown t 在改變到其它runlevel之前 告訴init多久以後關機。r 重啟電腦。k 並不真正關機 只是送警告訊號給每位登入者 login h 關機後關閉電源 halt n 不用init 而是自己來關機。不鼓勵使用這個選項 而且該選項所產生的後果往往不總是你所預期得到...