subprocess和logging模塊
阿新 • • 發佈:2017-09-06
span 結果 fin shell font user bsp findstr 安全
subprocess模塊
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解碼
logging模塊
用於便捷記錄日誌且線程安全的模塊
subprocess和logging模塊