網(wǎng)站首頁 編程語言 正文
一、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') )
原文鏈接:https://www.cnblogs.com/springsnow/p/11974749.html
相關推薦
- 2023-01-10 SpringEvent優(yōu)雅解耦時連續(xù)兩個bug的解決方案_Golang
- 2022-08-23 python使用redis模塊來跟redis實現(xiàn)交互_python
- 2022-02-25 DevTools failed to load SourceMap 警告處理方法
- 2022-07-06 Nginx的mirror指令示例配置_nginx
- 2022-03-26 C語言goto語句簡單使用詳解_C 語言
- 2022-12-21 C#?wpf?無邊框窗口添加陰影效果的實現(xiàn)_C#教程
- 2022-10-09 set_new_handler(0)有什么用_C 語言
- 2024-04-08 SpringBoot緩存注解@Cacheable、@CacheEvict和@CachePut
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支