目錄
- 一、Shutil 模塊
- 1.1 復(fù)制文件和文件夾
- 1.2 移動(dòng)文件和文件夾
- 1.3 刪除文件和文件夾
- 二、遍歷文件
- 三、壓縮文件
- 3.1 創(chuàng)建和添加ZIP文件
- 3.2 讀取ZIP文件
- 3.3 解壓縮ZIP文件
- 四、參考文獻(xiàn)
一、Shutil 模塊
shutil其實(shí)也就是shell模塊。其中包含一些函數(shù),可以讓我們?cè)趐ython程序中復(fù)制、移動(dòng)、改名和刪除文件。
1.1 復(fù)制文件和文件夾
- shutil.copy(source,destination):將路徑source處的文件復(fù)制到路徑destination處的文件夾。如果destination是一個(gè)文件名,那么它將作為被復(fù)制的新名字
- shutil.copytree(source,destination):將路徑source處的文件夾,包括它的所有文件和子文件夾,復(fù)制到路徑destination處的文件夾。
import shutil
import os
current_path = os.getcwd()
# 拷貝test.txt 文件到temp1文件夾下
shutil.copy(current_path+"/test.txt",current_path+"/Python/temp1")
# 將temp1文件夾內(nèi)容拷貝到temp2文件夾下,此時(shí)會(huì)創(chuàng)建temp2文件夾
shutil.copytree(current_path+"/Python/temp1",current_path+"/Python/temp2")
結(jié)果:

1.2 移動(dòng)文件和文件夾
- shutil.move(source,destination):將source處的文件夾移動(dòng)到路徑destination,并返回新位置的絕對(duì)路徑的字符串
注:
如果destination指向一個(gè)文件夾,source文件將移動(dòng)到destination中,并保持原來的文件名;
如果destination指向一個(gè)文件,這個(gè)文件就會(huì)被覆蓋,注意!
如果destination指向一個(gè)不存在的文件夾,這個(gè)時(shí)候source里面的內(nèi)容會(huì)移動(dòng)到destination,source改名為destination
import shutil
import os
current_path = os.getcwd()
# 將temp2文件夾內(nèi)的文件拷貝到temp中,并將temp2改名為temp
shutil.move(current_path+"/Python/temp2",current_path+"/temp")
結(jié)果:

1.3 刪除文件和文件夾
- os.unlink(path):將刪除path處的文件
- os.rmdir(path):將刪除path處的文件夾。該文件夾必須為空,其中沒有任何文件和文件夾
- shutil.retree(path):將刪除path處的文件夾,它包含的所有文件和文件夾都會(huì)被刪除
import shutil
import os
current_path = os.getcwd()
shutil.rmtree(current_path+"/temp")
結(jié)果:

二、遍歷文件
- os.walk(path):通過傳入一個(gè)路徑
os.walk()在循環(huán)的每次迭代中,返回三個(gè)值:
1.當(dāng)前文件夾名稱的字符串
2.當(dāng)前文件夾中子文件夾的字符串的列表
3.當(dāng)前文件夾中文件的字符串的列表
import shutil
import os
current_path = os.getcwd()
for folder_name,sub_folders,file_names in os.walk(current_path):
print(folder_name+":")
# 加載當(dāng)前文件路徑下的所有子文件
for sub_folder in sub_folders:
print("\t"+folder_name+": "+sub_folder+"(dir)")
for file_name in file_names:
print("\t"+folder_name+": "+file_name)
輸出(部分):
ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
/home/ubuntu/python_file:
/home/ubuntu/python_file: .vscode(dir)
/home/ubuntu/python_file: Python(dir)
/home/ubuntu/python_file: test.cpp
/home/ubuntu/python_file: test.txt
/home/ubuntu/python_file: tempCodeRunnerFile.py
/home/ubuntu/python_file/.vscode:
/home/ubuntu/python_file/.vscode: db(dir)
/home/ubuntu/python_file/.vscode: .git(dir)
/home/ubuntu/python_file/.vscode: log(dir)
/home/ubuntu/python_file/.vscode: settings.json
/home/ubuntu/python_file/.vscode/db:
/home/ubuntu/python_file/.vscode/db: cpptips.db-wal
/home/ubuntu/python_file/.vscode/db: cpptips.db-shm
/home/ubuntu/python_file/.vscode/db: cpptips.db
/home/ubuntu/python_file/.vscode/.git:
/home/ubuntu/python_file/.vscode/.git: 6eb7a60f73d1a1d9bdf44f2e86d7f4cc_test.cpp
/home/ubuntu/python_file/.vscode/log:
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-19.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-16.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-17.log
/home/ubuntu/python_file/.vscode/log: cpptips.client.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.log
/home/ubuntu/python_file/Python:
/home/ubuntu/python_file/Python: temp1(dir)
/home/ubuntu/python_file/Python: .git(dir)
/home/ubuntu/python_file/Python: README.md
/home/ubuntu/python_file/Python: hello_world.py
/home/ubuntu/python_file/Python: orignize_files.py
/home/ubuntu/python_file/Python: regex.py
/home/ubuntu/python_file/Python: file_mange.py
.........
.........
.........
三、壓縮文件
利用zipfile模塊中的函數(shù),python程序可以創(chuàng)建和打開ZIP文件。
3.1 創(chuàng)建和添加ZIP文件
想要?jiǎng)?chuàng)建ZIP文件,必須用ZipFile
方法創(chuàng)建一個(gè)ZipFile對(duì)象。ZipFile對(duì)象在概念上和File對(duì)象相似。
之后,以寫模式打開這個(gè)對(duì)象。調(diào)用write()
方法傳入一個(gè)路徑,python就會(huì)壓縮該路徑所指的文件,將它加到ZIP文件中。write的第一個(gè)參數(shù)是一個(gè)字符串,代表要添加的文件名。第二個(gè)參數(shù)是”壓縮類型“參數(shù),告訴計(jì)算機(jī)使用怎樣的算法來壓縮文件。一般來說,ZIP_DEFLATED就可以了。
import zipfile
import os
current_path = os.getcwd()
new_zip = zipfile.ZipFile("newZip","w")
new_zip.write("test.txt",compress_type=zipfile.ZIP_DEFLATED)
new_zip.write("test.cpp",compress_type=zipfile.ZIP_DEFLATED)
結(jié)果:

3.2 讀取ZIP文件
當(dāng)用ZipFile函數(shù)打開一個(gè)zip文件的時(shí)候,會(huì)返回一個(gè)ZipFile對(duì)象。之后調(diào)用這個(gè)對(duì)象的namelist()
方法就可以獲得zip里面的壓縮文件列表。
同時(shí)這個(gè)對(duì)象還有一個(gè)getinfo()方法,通過傳入一個(gè)壓縮文件名,就可以獲得這個(gè)文件的一些信息。
import zipfile
import os
current_path = os.getcwd()
new_zip = zipfile.ZipFile("newZip","r")
files = new_zip.namelist()
for file in files:
info = new_zip.getinfo(file)
print("filename: "+file)
print("\tsize: "+str(info.file_size))
print("\tcompress_size: "+str(info.compress_size))
輸出:
ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
filename: test.txt
size: 26
compress_size: 26
filename: test.cpp
size: 30
compress_size: 28
3.3 解壓縮ZIP文件
ZipFile對(duì)象的extractall方法從ZIP文件中解壓縮所有的文件和文件夾,放到當(dāng)前工作目錄下:
import zipfile
import os
current_path = os.getcwd()
example_zip = zipfile.ZipFile("newZip","r")
# 解壓
example_zip.extractall()
example_zip.close()
結(jié)果:

四、參考文獻(xiàn)
AI Sweigart.Python編程快速上手——讓繁瑣工作自動(dòng)化.人民郵電出版社.2016.07
到此這篇關(guān)于python基礎(chǔ)學(xué)習(xí)之組織文件的文章就介紹到這了,更多相關(guān)python組織文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python基礎(chǔ)之文件處理知識(shí)總結(jié)
- Python關(guān)于OS文件目錄處理的實(shí)例分享
- python引入其他文件夾下的py文件具體方法
- 解決python中os.system調(diào)用exe文件的問題
- python使用glob檢索文件的操作
- 教你用Python代碼實(shí)現(xiàn)合并excel文件
- python3 hdf5文件 遍歷代碼
- Python基礎(chǔ)之元組與文件知識(shí)總結(jié)
- python提取word文件中的所有圖片
- python可視化hdf5文件的操作
- Python 如何讀取.txt,.md等文本文件
- 教你利用Python破解ZIP或RAR文件密碼
- Python文件基本操作實(shí)用指南
- Python爬蟲之m3u8文件里提取小視頻的正確姿勢
- python 如何把classification_report輸出到csv文件
- 用python刪除文件夾中的重復(fù)圖片(圖片去重)
- 將Python代碼打包成.exe可執(zhí)行文件的完整步驟
- Python文件名的匹配之clob庫