python实践项目(九)

来源:互联网 发布:361彩票是网络诈骗吗 编辑:程序博客网 时间:2024/06/08 19:33

项目:将一个文件夹备份到一个 ZIP 文件

假定你正在做一个项目,它的文件保存在C:\AlsPythonBook文件夹中。你担心工作会丢失, 所以希望为整个文件夹创建一个ZIP文件, 作为“快照”。你希望保存不同的版本, 希望ZIP文件的文件名每次创建时都有所变化。例如AlsPythonBook_1.zipAlsPythonBook_2.zipAlsPythonBook_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')

先完成基本任务: 添加#!行, 描述该程序做什么,并导入zipfileos 模块。定义backupToZip()函数,它只接收一个参数,即folder。这个参数是一个字符串路径, 指向需要备份的文件夹。该函数将决定它创建的ZIP 文件使用什么文件名,然后创建该文件,遍历folder文件夹, 将每个子文件夹和文件添加到ZIP文件中。在源代码中为这些步骤写下TODO注释, 提醒你稍后来完成。第一部分命名这个ZIP文件, 使用 folder 的绝对路径的基本名称。如果要备份的文件夹是C:\deliciousZIP 文件的名称就应该是 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')