python实践项目(九)
来源:互联网 发布:361彩票是网络诈骗吗 编辑:程序博客网 时间:2024/06/08 19:33
项目:将一个文件夹备份到一个 ZIP 文件
假定你正在做一个项目,它的文件保存在C:\AlsPythonBook文件夹中。你担心工作会丢失, 所以希望为整个文件夹创建一个ZIP文件, 作为“快照”。你希望保存不同的版本, 希望ZIP文件的文件名每次创建时都有所变化。例如AlsPythonBook_1.zip、AlsPythonBook_2.zip、AlsPythonBook_3.zip, 等等。你可以手工完成, 但这有点烦人,而且可能不小心弄错ZIP 文件的编号。运行一个程序来完成这个烦人的任务会简单得多。针对这个项目,打开一个新的文件编辑器窗口, 将它保存为backupToZip.py。
第 1 步:弄清楚 ZIP 文件的名称
这个程序的代码将放在一个名为backupToZip()的函数中。这样就更容易将该函数复制粘贴到其他需要这个功能的Python程序中。在这个程序的末尾, 会调用这个函数进行备份。让你的程序看起来像这样:
#! python3# backupToZip.py - Copies an entire folder and its contents into# a ZIP file whose filename increments.import zipfile, osdef 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 should 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:\\delicious')
先完成基本任务: 添加#!行, 描述该程序做什么,并导入zipfile和 os 模块。定义backupToZip()函数,它只接收一个参数,即folder。这个参数是一个字符串路径, 指向需要备份的文件夹。该函数将决定它创建的ZIP 文件使用什么文件名,然后创建该文件,遍历folder文件夹, 将每个子文件夹和文件添加到ZIP文件中。在源代码中为这些步骤写下TODO注释, 提醒你稍后来完成。第一部分命名这个ZIP文件, 使用 folder 的绝对路径的基本名称。如果要备份的文件夹是C:\delicious,ZIP 文件的名称就应该是 delicious_N.zip, 第一次运行该程序时N=1,第二次运行时N=2, 以此类推。通过检查 delicious_1.zip是否存在, 然后检查delicious_2.zip是否存在, 继续下去, 可以确定N应该是什么。 用一个名为 number的变量表示 N, 在一个循环内不断增加它, 并调用os.path.exists()来检查该文件是否存在。 第一个不存在的文件名将导致循环break,因此它就发现了新ZIP 文件的文件名。
第 2 步:创建新 ZIP 文件
接下来让我们创建 ZIP 文件。让你的程序看起来像这样:
#! python3# backupToZip.py - Copies an entire folder and its contents into# a ZIP file whose filename increments.--snip-- while True: zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip' if not os.path.exists(zipFilename): break number = number + 1# 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:\\delicious')既然新ZIP 文件的文件名保存在zipFilename变量中, 你就可以调用zipfile.ZipFile(),实际创建这个ZIP文件。确保传入'w'作为第二个参数, 这样ZIP文件以写模式打开。
第 3 步:遍历目录树并添加到 ZIP 文件
现在需要使用 os.walk()函数,列出文件夹以及子文件夹中的每个文件。让你的程序看起来像这样:
#! python3# 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)# Add all the files in this folder to the ZIP file. for filename in filenames: newBase = os.path.basename(folder) + '_' if filename.startswith(newBase) and filename.endswith('.zip') continue # don't backup the backup ZIP files backupZip.write(os.path.join(foldername, filename)) backupZip.close() print('Done.')
backupToZip('C:\\delicious')可以在for 循环中使用os.walk(),在每次迭代中,它将返回这次迭代当前的文件夹名称、 这个文件夹中的子文件夹,以及这个文件夹中的文件名。在这个for循环中,该文件夹被添加到 ZIP 文件。嵌套的for循环将遍filenames列表中的每个文件。每个文件都被添加到ZIP文件中, 以前生成的备份ZIP文件除外。如果运行该程序,它产生的输出看起来像这样:
Creating delicious_1.zip...Adding files in C:\delicious...Adding files in C:\delicious\cats...Adding files in C:\delicious\waffles...Adding files in C:\delicious\walnut...Adding files in C:\delicious\walnut\waffles...Done.第二次运行它时,它将C:\delicious 中的所有文件放进一个ZIP文件, 命名为delicious_2.zip, 以此类推。
第 4 步:类似程序的想法
你可以在其他程序中遍历一个目录树, 将文件添加到压缩的ZIP归档文件中。
例如,你可以编程做下面的事情:
• 遍历一个目录树, 将特定扩展名的文件归档, 诸如.txt或.py,并排除其他文件。
• 遍历一个目录树,将除.txt和.py文件以外的其他文件归档。
• 在一个目录树中查找文件夹,它包含的文件数最多,或者使用的磁盘空间最大。
完整程序:
#-*-coding:utf-8-*-#! python3# backupToZip - Copies an entire folder and its contents into# a ZIP file whose filename incrementsimport zipfile,osdef 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 should 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 += 1 # Create the ZIP file. print('Creating %s...'%(zipFilename)) backupZip = zipfile.ZipFile(zipFilename,'w') # 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 filename in filenames: newBase = os.path.basename(folder) + '_' if filename.startswith(newBase) and filename.endswith('.zip'): continue backupZip.write(os.path.join(foldername,filename)) backupZip.close() print('Done.')backupToZip('C:\\delicious')
- python实践项目(九)
- python实践项目(三)
- python实践项目(四)
- python实践项目(五)
- python实践项目(六)
- python实践项目(七)
- python实践项目(八)
- Maven实践(九)---项目的聚合和继承
- 【Netty4 简单项目实践】九、示例项目
- python实践项目(一)
- python实践项目(二)
- python计算机视觉项目实践
- python 文件读写项目实践
- 缺少python实践项目怎么办
- Python爬虫实践(九):第一个爬虫实例(简书首页)
- 实践项目四:桌面版python 查询天气(3)
- 实践项目十一:Python批量图片切割(PIL)
- ios项目(九)
- 从键盘输入数据并显示
- 排序算法总结-Shell排序、归并排序、快排、堆排
- IO流(文件复制的方法)
- Ubuntu绑定静态ip
- Git相关参考文档
- python实践项目(九)
- This的实例化
- BZOJ 4513: [Sdoi2016]储能表 数位dp
- js跨域及解决
- popen函数
- INSTALL_FAILED_NO_MATCHING_ABIS 的解决办法
- [log]2017-11-15
- caffe源码阅读之Blob
- C交换数组