python3 呼叫 centos 常用系統命令

2022-09-06 01:48:09 字數 1396 閱讀 8263

一、建立目錄

1

importos2

3 base_path = '

/data/sw_backup

'4 addr= 'ft'

5 ip='

192.168.1.1

'6 path = base_path+'

/'+addr+'

/'+ip #

合併路徑

7 os.makedirs(path,0o777,exist_ok=true) #

建立檔案,不存在的目錄直接建立,類似 mkdir -p $path 注:此命令中直接賦權不生效。 777 沒用

二、修改目錄許可權

1

import

subprocess

23 base_path = '

/data/sw_backup

'4 cmd = ['

chmod

', "

-r", "

777"

, base_path]

5 res = subprocess.run(cmd, universal_newlines=true, stdout=subprocess.pipe,stderr=subprocess.pipe, shell=false) #

將目錄下所有目錄檔案全部賦權777

6print(res.returncode, res.stdout, res.stderr)

三、刪除檔案

1

importos2

3if os.path.exists(key_zip): #

如果檔案存在

4  #

刪除檔案

5  os.remove(key_zip)

四、刪除資料夾及資料夾中所有檔案

1

import

shutil

23 base_path='

/data/sw_backup

'4 addr='ft'

5 ip = '

192.168.0.1

'6 sw_folder = base_path+'

/'+addr+'

/'+ip78

ifos.path.exists(sw_folder):

9print

(sw_folder)

10 shutil.rmtree(sw_folder) #

刪除資料夾及檔案中的所有內容

五、讀取檔案

日常Python 3 呼叫函式

任務.輸出每日一貼 在idle中建立乙個名為function tips.py的檔案,然後在該檔案中建立乙個名稱為function tips的函式,在該函式中,從勵志文字列表中獲取一條勵志文字並輸出,最後再呼叫函式function tips 如下 coding gbk def function tip...

python3呼叫http介面

最近在寫python呼叫介面的服務,寫了乙個呼叫介面的函式,如下 param inputdata 單個樣本的輸入引數,是json格式的資料 return 單個樣本的探真查詢變數結果資料 呼叫介面 介面有正確的資料才讀入,否則為空 if res code 0000 res data json.load...

Python3呼叫Google翻譯

import urllib.request from handlejs import py4js def open url url headers req urllib.request.request url url,headers headers response urllib.request.u...