網(wǎng)站首頁 編程語言 正文
今天的章節(jié)我們來學(xué)習(xí)一下文件的裁剪、壓縮與解壓縮。所謂的文件裁剪就是從目前文件路徑A移動到目標(biāo)文件路徑B ,A 與 B可能是相同的,也有可能是不同的。當(dāng)目標(biāo)移動之后,A 路徑下就不存在這個文件了,只存在目標(biāo)路徑 B 下。但是也支持目標(biāo) A 下的名稱進(jìn)行改變,所以它也是一個變相的重命名。至于壓縮與解壓縮,這里就不需要過多的語言解釋了吧… 都懂的…
利用 shutil 實現(xiàn)文件的裁剪(移動、重命名)
導(dǎo)入包與模塊
from shutil import move
使用方法:
move(來源地址, 目標(biāo)地址)
結(jié)合我們上一章節(jié)的測試腳本場景,將 abcd.txt 移動到 test01 目錄下,然后再將 abcd_copy.txt 在當(dāng)前目錄下重命名為 efg_copy.txt。
代碼示例如下:
# coding:utf-8
import os
from shutil import move
path = os.path.join(os.getcwd(), 'abcd.txt') # 利用 os 模塊的 getcwd() 函數(shù) 分別獲取絕對路徑
target = os.path.join(os.getcwd(), 'test01')
move(path, target) # 將 "abcd.txt" 文件移動到 "test01" 目錄下
move('abcd_copy.txt', 'efg_copy.txt') # 將 "abcd_copy.txt" 重命名為 "efg_copy.txt"
運(yùn)行結(jié)果如下:
文件的刪除
這里我們將使用到 os 包的 remove() 函數(shù)實現(xiàn)文件的刪除操作,在 shutil 包中是沒有包含單獨的文件刪除的函數(shù)的,是因為 os 包的刪除已經(jīng)足夠細(xì)致了。
接下來我們再鞏固一下 os 包的remove() 函數(shù),利用 remove() 函數(shù)刪除上文我們重名后的 efg.txt 文件:
# coding:utf-8
import os
from shutil import copy, copyfile, move
path = os.path.join(os.getcwd(), 'abcd.txt') # 利用 os 模塊的 getcwd() 函數(shù) 分別獲取絕對路徑
target = os.path.join(os.getcwd(), 'test01')
# copyfile(path, target)
# move(path, target) # 將 "abcd.txt" 文件移動到 "test01" 目錄下
# move('abcd_copy.txt', 'efg_copy.txt') # 將 "abcd_copy.txt" 重命名為 "efg_copy.txt"
os.remove('efg_copy.txt')
運(yùn)行結(jié)果如下:
利用 shutil 實現(xiàn)文件的壓縮
讓我們先看看壓縮的函數(shù):
導(dǎo)入包與模塊
from shutil import make_archive
使用方法
make_archive(壓縮之后的文件名, 壓縮文件的后綴, 希望被壓縮的文件或目錄)
返回值
生成的壓縮包地址
import os
from shutil import copy, copyfile, move, make_archive
path = os.path.join(os.getcwd(), 'abcd.txt') # 利用 os 模塊的 getcwd() 函數(shù) 分別獲取絕對路徑
target = os.path.join(os.getcwd(), 'test01')
make_archive('test01', 'zip', os.path.join(os.getcwd(), 'test01'))
運(yùn)行結(jié)果如下:
利用 shutil 實現(xiàn)文件的解壓縮
讓我們再看看解壓縮的函數(shù):
導(dǎo)入包與模塊
from shutil import unpack_archive
使用方法
unpack_archive(要解壓的文件, 解壓后的路徑)
返回值
生成的壓縮包地址
代碼示例如下:
# coding:utf-8
import os
from shutil import copy, copyfile, move, make_archive, unpack_archive
# path = os.path.join(os.getcwd(), 'abcd.txt') # 利用 os 模塊的 getcwd() 函數(shù) 分別獲取絕對路徑
# target = os.path.join(os.getcwd(), 'test01')
# make_archive('test01', 'zip', os.path.join(os.getcwd(), 'test01'))
target = os.path.join(os.getcwd(), 'test02') # 為了區(qū)分解壓之后的目錄名稱與之前的test01,這里使用test02 作為解壓后的目錄
unpack_archive('test01.zip', target)
運(yùn)行結(jié)果如下:
原文鏈接:https://blog.csdn.net/weixin_42250835/article/details/124580184
相關(guān)推薦
- 2022-07-15 C語言函數(shù)棧幀的創(chuàng)建與銷毀原理圖解_C 語言
- 2022-11-30 深入理解Golang?channel的應(yīng)用_Golang
- 2022-04-16 python多線程互斥鎖與死鎖_python
- 2022-07-01 python神經(jīng)網(wǎng)絡(luò)ShuffleNetV2模型復(fù)現(xiàn)詳解_python
- 2022-12-28 QT?Creator+OpenCV實現(xiàn)圖像灰度化的示例代碼_C 語言
- 2023-03-22 利用tkinter改變下拉列表(Combobox)的選項值_python
- 2022-04-12 python關(guān)閉print輸出信息詳情_python
- 2022-10-25 如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支