Python使用shutil操作文件、subprocess運行子程序
一、shutil模塊(了解):高級的文件、文件夾、壓縮包處理模塊。
import shutil # shutil.copyfileobj(fsrc, fdst[, length]),將文件內(nèi)容拷貝到另一個文件中 shutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w')) # shutil.copyfile(src, dst),拷貝文件 shutil.copyfile('f1.log', 'f2.log') # 目標文件無需存在 # shutil.copymode(src, dst),僅拷貝權(quán)限。內(nèi)容、組、用戶均不變 shutil.copymode('f1.log', 'f2.log') # 目標文件必須存在 # shutil.copystat(src, dst),僅拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags shutil.copystat('f1.log', 'f2.log') # 目標文件必須存在 # shutil.copy(src, dst),拷貝文件和權(quán)限 shutil.copy('f1.log', 'f2.log') # shutil.copy2(src, dst),拷貝文件和狀態(tài)信息 shutil.copy2('f1.log', 'f2.log') # shutil.ignore_patterns(*patterns) # shutil.copytree(src, dst, symlinks=False, ignore=None),遞歸的去拷貝文件夾 # 目標目錄不能存在,注意對folder2目錄父級目錄要有可寫權(quán)限,ignore的意思是排除 shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) # shutil.rmtree(path[, ignore_errors[, onerror]]),遞歸的去刪除文件 shutil.rmtree('folder1') # shutil.move(src, dst),遞歸的去移動文件,它類似mv命令,其實就是重命名 shutil.move('folder1', 'folder3') # shutil.make_archive(base_name, format, ...),創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar ''' base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時,則保存至當前目錄,否則保存至指定路徑,如 data_bak = >保存至當前路徑;/ tmp/data_bak = >保存至/tmp/ format:壓縮包種類,“zip”, “tar”, “bztar”,“gztar” root_dir:要壓縮的文件夾路徑(默認當前目錄) owner:用戶,默認當前用戶 group:組,默認當前組 logger:用于記錄日志,通常是logging.Logger對象 ''' # 將 /data 下的文件打包放置當前程序目錄 ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') # 將 /data下的文件打包放置 /tmp/目錄 ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
shutil 對壓縮包的處理是調(diào)用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:
1、 zipfile壓縮解壓縮
import zipfile # 壓縮 z = zipfile.ZipFile('laxi.zip', 'w') z.write('a.log') z.write('data.data') z.close() # 解壓 z = zipfile.ZipFile('laxi.zip', 'r') z.extractall(path='.') z.close()
2、 tarfile壓縮解壓縮
import tarfile # 壓縮 t=tarfile.open('/tmp/egon.tar','w') t.add('/test1/a.py',arcname='a.bak') t.add('/test1/b.py',arcname='b.bak') t.close() # 解壓 t=tarfile.open('/tmp/egon.tar','r') t.extractall('/egon') t.close()
二、subprocess模塊(了解):運行子程序
subprocess模塊允許你去創(chuàng)建一個新的進程讓其執(zhí)行另外的程序,并與它進行通信,獲取標準的輸入、標準輸出、標準錯誤以及返回碼等。
更多查看官網(wǎng):https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments
import subprocess ''' sh-3.2# ls /Users/nick/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')) # 下面這段等同于上面,但是上面的優(yōu)勢在于,一個數(shù)據(jù)流可以和另外一個數(shù)據(jù)流交互,可以通過爬蟲得到結(jié)果然后交給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$' res1 = subprocess.Popen(r'dir “C:\Users\Administrator\PycharmProjects\test\函數(shù)備課”', shell=True, stdout=subprocess.PIPE) res = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE) # subprocess使用當前系統(tǒng)默認編碼,得到結(jié)果為bytes類型,在windows下需要用gbk解碼 print(res.stdout.read().decode('gbk') )
到此這篇關(guān)于Python使用shutil操作文件、subprocess運行子程序的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python利用shutil模塊實現(xiàn)文件夾的復(fù)制刪除與裁剪
- Python利用shutil模塊實現(xiàn)文件的裁剪與壓縮
- Python中shutil模塊的使用詳解
- Python 中 Shutil 模塊詳情
- python shutil操作文件實例講解
- 解決python subprocess參數(shù)shell=True踩到的坑
- Python中判斷subprocess調(diào)起的shell命令是否結(jié)束
- python subprocess pipe 實時輸出日志的操作
- 使用python執(zhí)行shell腳本 并動態(tài)傳參 及subprocess的使用詳解
- python中的subprocess.Popen()使用詳解
相關(guān)文章
python unittest實現(xiàn)api自動化測試
這篇文章主要為大家詳細介紹了python unittest實現(xiàn)api自動化測試的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Python神器之使用watchdog監(jiān)控文件變化
這篇文章主要為大家詳細介紹了Python中的神器watchdog以及如何使用watchdog監(jiān)控文件變化,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2023-12-12python3.x+pyqt5實現(xiàn)主窗口狀態(tài)欄里(嵌入)顯示進度條功能
這篇文章主要介紹了python3.x+pyqt5實現(xiàn)主窗口狀態(tài)欄里(嵌入)顯示進度條功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07python輕松辦公將100個Excel中符合條件的數(shù)據(jù)匯總到1個Excel里
這篇文章主要為大家介紹了python輕松辦公將100個Excel中符合條件的數(shù)據(jù)匯總到1個Excel里示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03