網站首頁 編程語言 正文
一、shutil模塊(了解):高級的文件、文件夾、壓縮包處理模塊。
import shutil
# shutil.copyfileobj(fsrc, fdst[, length]),將文件內容拷貝到另一個文件中
shutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w'))
# shutil.copyfile(src, dst),拷貝文件
shutil.copyfile('f1.log', 'f2.log') # 目標文件無需存在
# shutil.copymode(src, dst),僅拷貝權限。內容、組、用戶均不變
shutil.copymode('f1.log', 'f2.log') # 目標文件必須存在
# shutil.copystat(src, dst),僅拷貝狀態的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') # 目標文件必須存在
# shutil.copy(src, dst),拷貝文件和權限
shutil.copy('f1.log', 'f2.log')
# shutil.copy2(src, dst),拷貝文件和狀態信息
shutil.copy2('f1.log', 'f2.log')
# shutil.ignore_patterns(*patterns)
# shutil.copytree(src, dst, symlinks=False, ignore=None),遞歸的去拷貝文件夾
# 目標目錄不能存在,注意對folder2目錄父級目錄要有可寫權限,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, ...),創建壓縮包并返回文件路徑,例如: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 對壓縮包的處理是調用 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模塊允許你去創建一個新的進程讓其執行另外的程序,并與它進行通信,獲取標準的輸入、標準輸出、標準錯誤以及返回碼等。
更多查看官網: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'))
# 下面這段等同于上面,但是上面的優勢在于,一個數據流可以和另外一個數據流交互,可以通過爬蟲得到結果然后交給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\函數備課”', shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE)
# subprocess使用當前系統默認編碼,得到結果為bytes類型,在windows下需要用gbk解碼
print(res.stdout.read().decode('gbk') )
原文鏈接:https://www.cnblogs.com/springsnow/p/11974749.html
相關推薦
- 2022-04-15 python中對正則表達式re包的簡單引用方式_python
- 2023-10-14 uni-app adb安卓wifi無線調試
- 2022-08-20 使用Docker制作Python環境連接Oracle鏡像_python
- 2022-06-12 一文搞懂C語言static關鍵字的三個作用_C 語言
- 2022-08-07 Python繪制交通流折線圖詳情_python
- 2022-05-06 Sqlite 刪除數據后為什么文件大小不變
- 2023-11-21 python使用print輸出不同顏色的字體、終端顯示不同顏色字體
- 2022-12-29 Python?Setuptools的?setup.py實例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支