Python编程快速上手-组织文件
-
shutil模块
复制文件和文件夹
shutil.copy() 复制一个文件,shutil.copytree()将复制整个文件夹
1 2 3 4 5 6 | >>> import shutil, os >>> os.chdir( 'C:\\PyProjects' ) >>> shutil.copy( 'C:\\PyProjects\\this.txt' , 'C:\\PyProjects\\this_copy.txt' ) 'C:\\PyProjects\\this_copy.txt' >>> shutil.copytree( 'bacon' , 'bacon_copy' ) 'bacon_copy' |
文件和文件夹的移动与改名
shutil.move(source, destination)
1 2 3 4 5 6 | >>> import shutil >>> dir <built - in function dir > >>> import shutil >>> shutil.move( 'C:\\PyProjects\\bacon.txt' , 'C:\\PyProjects\\bacon' ) 'C:\\PyProjects\\bacon\\bacon.txt' |
改名
1 2 | >>> shutil.move( 'C:\\PyProjects\\bacon\\bacon.txt' , 'C:\\PyProjects\\bacon\\new_bacon.txt' ) 'C:\\PyProjects\\bacon\\new_bacon.txt' |
永久删除文件和文件夹
利用os模块中的函数,可以删除一个文件或一个空文件夹,但利用shutil模块,可以删除一个文件夹及其所有的内容。
- 用os.unlink(path)将删除path处的文件
- os.rmdir(path) 将删除path处的文件夹,该文件夹必须为空,没有任何文件和文件夹
- shutil.rmtree(path)将删除path处的文件夹,它包含所有文件和文件夹都会被删除
1 2 3 4 5 | import os for filename in os.listdir( 'C:\\PyProjects' ): if filename.endswith( '.txt' ): print (filename) #os.unlink(filename) |
1 2 3 | C:\PyProjects>C: / Python37 / python3.exe c: / PyProjects / demo.py bacon.txt this.txt |
用send2trash模式安全地删除
安装send2trash
1 2 3 4 5 6 7 | C:\Users\ElonTian>pip3 install send2trash Collecting send2trash Downloading Send2Trash - 1.8 . 0 - py3 - none - any .whl ( 18 kB) Installing collected packages: send2trash Successfully installed send2trash - 1.8 . 0 WARNING: You are using pip version 21.1 . 2 ; however, version 22.0 . 4 is available. You should consider upgrading via the 'c:\python37\python3.exe -m pip install --upgrade pip' command. |
利用send2trash,比Python常规的删除函数要安全得多,因为它将文件夹和文件发送到计算机的垃圾箱或回收站,而不是永久删除。
1 2 3 4 5 6 7 8 | >>> import send2trash >>> os.chdir( 'C:\\PyProjects' ) >>> baconFile = open ( 'bacon.txt' , 'a' ) >>> baconFile.write( 'Bacon is not a vegetable.' ) 25 >>> baconFile.close() >>> send2trash.send2trash( 'bacon.txt' ) >>> |
send2trash.send2trash()函数删除文件和文件夹,虽然它将文件发送到垃圾箱,稍后能够恢复它们,但这不像永久删除文件,不会释放磁盘空间。这时,os和shutil可以删除文件和文件夹,释放磁盘空间。注意,send2trash()函数只能将文件送到垃圾箱,不能从中恢复文件。
-
遍历目录树
os.walk() 函数被传入一个字符串值,即一个文件夹的路径。在一个for循环语句中使用os.walk()函数,遍历目录树。os.walk()在循环的每次迭代中,返回3个值:
- 1、当前文件夹名称的字符串
- 2、当前文件夹中子文件夹的列表
- 3、当前文件夹中文件的字符串的列表
1 2 3 4 5 6 7 8 | import os for folderName, subfolders, filenames in os.walk( 'C:\\PyProjects' ): print ( 'The current folders is ' + folderName) for subfolder in subfolders: print ( 'SUBFOLDER OF ' + folderName + ': ' + subfolder) for filename in filenames: print ( 'FILE INSIDE ' + folderName + ': ' + filename) print ('') |
-
用zipfile模块压缩文件
利用zipfile模块中的函数,Python程序可以创建和打开(解压)ZIP文件。假定,example.zip的文件,可以从http://nostarch.com/automatestuff/下载文件。
读取ZIP文件
要读取ZIP文件的内容,首先必须创建一个ZipFile对象 。调用zipfile.ZipFile()函数,向它传入一个字符串,表示.zip文件的文件名,请注意,zipfile是Python模块的名称,ZipFile() 是函数的名称。
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> import zipfile, os >>> os.chdir( 'C:\\PyProjects' ) >>> exampleZip = zipfile.ZipFile( 'example.zip' ) >>> exampleZip.namelist() [ 'spam.txt' , 'cats/' , 'cats/catnames.txt' , 'cats/zophie.jpg' ] >>> spamInfo = exampleZip.getinfo( 'spam.txt' ) >>> spamInfo.file_size 13908 >>> spamInfo.compress_size 3828 >>> 'Compressed file is %sx smaller!' % ( round (spamInfo.file_size / spamInfo.compress_size, 2 )) 'Compressed file is 3.63x smaller!' >>> exampleZip.close() |
ZipFile对象的namelist()方法,返回ZIP文件中包含的所有文件和文件夹的字符串的列表。ZipInfo对象有自己的属性,诸如表示字节数的file_size和compress_size。分别表示原来文件大小和压缩后文件大小。ZipFile 对象表示整个归档文件,而ZipInfo 对象则保存该归档文件中每个文件的有用信息。
从ZIP文件中解压缩
extractall() 方法从ZIP文件解压缩所有文件和文件夹,放到当前工作目录
1 2 3 4 5 | >>> import zipfile, os >>> os.chdir( 'C:\\PyProjects' ) >>> exampleZip = zipfile.ZipFile( 'example.zip' ) >>> exampleZip.extractall() >>> exampleZip.close() |
ZipFile对象的extract()方法从ZIP文件中解压缩单个文件
1 2 3 4 5 6 | >>> exampleZip = zipfile.ZipFile( 'example.zip' ) >>> exampleZip.extract( 'spam.txt' ) 'C:\\PyProjects\\spam.txt' >>> exampleZip.extract( 'spam.txt' , 'C:\\PyProjects\\cats' ) 'C:\\PyProjects\\cats\\spam.txt' >>> exampleZip.close() |
传递给extract()的字符串,必须匹配namelist()返回的字符串列表中的一个。或者可以向extract() 传递第二个参数,将文件解压缩到指定的文件夹,而不是当前工作目录。如果第二个参数指定的文件夹不存在,Python就会创建它,extract() 的返回值是被压缩后文件的绝对路径。
创建和添加到ZIP文件
zipfile.ZIP_DEFLATED,指定deflate压缩算法,对各种类型的数据很有效。
1 2 3 4 | >>> import zipfile >>> newZip = zipfile.ZipFile( 'new.zip' , 'w' ) >>> newZip.write( 'spam.txt' , compress_type = zipfile.ZIP_DEFLATED) >>> newZip.close() |
-
将一个文件夹备份到一个zip文件
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #!/usr/bin/env python # backupToZip.py - Copies an entire folder and its contents into a ZIP file whose filename increments. import zipfile, os def backupToZip(folder): # Backup the entire contents of "folder" into a ZIP file. folder = os.path.abspath(folder) # make sure folder is absolute # Figure out the filename this code shoulde use based on # what files already exist. number = 1 while True : zipFilename = os.path.basename(folder) + '_' + str (number) + '.zip' if not os.path.exists(zipFilename): break number = number + 1 # TODO: Create the ZIP file. print ( 'Creating %s...' % (zipFilename)) backupZip = zipfile.ZipFile(zipFilename, 'w' ) # TODO: Walk the entire folder tree and compress the files in each folder. for foldername, subfolders, filenames in os.walk(folder): print ( 'Adding files in %s...' % (foldername)) # Add the current folder to the ZIP file. backupZip.write(foldername) # Add all the files in this folder to the ZIP file. for filename in filenames: if filename.startswith(os.path.basename(folder) + '_' ) and filename.endswith( '.zip' ): continue # don't backup the backup ZIP file backupZip.write(os.path.join(foldername, filename)) backupZip.close() print ( 'Done.' ) backupToZip( 'C:\\PyProjects' ) |
运行结果:
1 2 3 4 5 6 7 | C:\PyProjects>C: / Python37 / python3.exe c: / PyProjects / demo.py Creating PyProjects_1. zip ... Adding files in C:\PyProjects... Adding files in C:\PyProjects\.vscode... Adding files in C:\PyProjects\zabbix... Adding files in C:\PyProjects\__pycache__... Done. |
第1步:弄清楚ZIP文件的名称
定义backupToZip()函数,它只接收一个参数,即folder。这个参数是一个字符串路径,指向需要备份的文件夹。该函数将决定它创建的ZIP文件使用什么文件名,然后创建该文件,遍历folder文件夹,将每个子文件夹和文件添加到ZIP文件中。
os.path.exists() 检查备份文件是否存在,第一个不存在的文件名将导致循环break,因此它会发现新ZIP文件的文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import zipfile, os def backupToZip(folder): # Backup the entire contents of "folder" into a ZIP file. folder = os.path.abspath(folder) # make sure folder is absolute # Figure out the filename this code shoulde use based on # what files already exist. number = 1 while True : zipFilename = os.path.basename(folder) + '_' + str (number) + '.zip' if not os.path.exists(zipFilename): break number = number + 1 # TODO: Create the ZIP file. # TODO: Walk the entire folder tree and compress the files in each folder. print ( 'Done.' ) backupToZip( 'C:\\PyProjects' ) |
第2步:创建新ZIP文件
zipfile.ZipFile()创建ZIP文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/usr/bin/env python # backupToZip.py - Copies an entire folder and its contents into a ZIP file - - snip - - while True : zipFilename = os.path.basename(folder) + '_' + str (number) + '.zip' if not os.path.exists(zipFilename): break number = number + 1 # TODO: Create the ZIP file. print ( 'Creating %s...' % (zipFilename)) backupZip = zipfile.ZipFile(zipFilename, 'w' ) # TODO: Walk the entire folder tree and compress the files in each folder. print ( 'Done.' ) backupToZip( 'C:\\PyProjects' ) |
第3步:遍历目录树并添加到ZIP文件
os.walk()函数,列出文件夹以及文件夹中的每个文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/usr/bin/env python # backupToZip.py - Copies an entire folder and its contents into a ZIP file whose filename increments. - - snip - - # Walk the entire folder tree and compress the files in each folder. for foldername, subfolders, filenames in os.walk(folder): # 每次迭代,它将返回这次迭代当前的文件夹名称、这个文件夹的子文件夹,已经这个文件夹中的文件名 print ( 'Adding files in %s...' % (foldername)) # Add the current folder to the ZIP file. backupZip.write(foldername) #在for循环中,该文件夹被添加到ZIP文件 # Add all the files in this folder to the ZIP file. for filename in filenames: #遍历filenames列表中的每个文件,每个文件都被添加到ZIP文件中,以前生成的备份ZIP除外 if filename.startswith(os.path.basename(folder) + '_' ) and filename.endswith( '.zip' ): continue # don't backup the backup ZIP file backupZip.write(os.path.join(foldername, filename)) backupZip.close() print ( 'Done.' ) backupToZip( 'C:\\PyProjects' ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!