python模組之subprocess模組

2021-08-21 12:48:46 字數 1003 閱讀 7134

import  subprocess

'''sh-3.2# ls /users/egon/desktop |grep txt$

mysql.txt

tt.txt

事物.txt

'''res1=subprocess.popen('ls /users/jieli/desktop',shell=true,stdout=subprocess.pipe)

res=subprocess.popen('grep txt$',shell=true,stdin=res1.stdout,

stdout=subprocess.pipe)

print(res.stdout.read().decode('utf-8'))

#等同於上面,但是上面的優勢在於,乙個資料流可以和另外乙個資料流互動,可以通過爬蟲得到結果然後交給grep

res1=subprocess.popen('ls /users/jieli/desktop |grep txt$',shell=true,stdout=subprocess.pipe)

print(res1.stdout.read().decode('utf-8'))

#windows下:

# dir | findstr 'test*'

# dir | findstr 'txt$'

import subprocess

res1=subprocess.popen(r'dir c:\users\administrator\pycharmprojects\test\函式備課',shell=true,stdout=subprocess.pipe)

res=subprocess.popen('findstr test*',shell=true,stdin=res1.stdout,

stdout=subprocess.pipe)

print(res.stdout.read().decode('gbk')) #subprocess使用當前系統預設編碼,得到結果為bytes型別,在windows下需要用gbk解碼

python模組之shutil模組

高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 import shutil shutil.copyfileobj open old.xml r open new.xml w shutil.copyfile ...

python模組之timeit模組

timeit模組用來測量函式執行時間,通過實際 學習怎樣應用timeit模組 fromtimeitimport print timeit x 7 print timeit x 7 number 1000000 print timeit x 7 number 1000000 print 上面三個列印說...

python模組 之 re模組

功能 實現python對正規表示式對支援與應用,將想要得到對內容按照正規表示式匹配出來 應用場景 爬蟲指令碼 對使用者輸入內容進行合規檢查 如qq格式檢查 等 功能 匹配物件中所有符合正規表示式的內容,並取出來 返回值 列表,所匹配到對項都會返回到列表中 import re content 1362...