python练习项目五——将一个文件夹备份到一个ZIP 文件

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

背景

假定你正在做一个项目,它的文件保存在C:\AlsPythonBook 文件夹中。你担心工作会丢失,所以希望为整个文件夹创建一个ZIP 文件,作为“快照”。你希望保存不同的版本,希望ZIP 文件的文件名每次创建时都有所变化。例如AlsPythonBook_1.zip、AlsPythonBook_2.zip、AlsPythonBook_3.zip,等等。你可以手工完成,但这有点烦人,而且可能不小心弄错ZIP 文件的编号。运行一个程序来完成这个烦人的任务会简单得多。

参考思路

这个程序的代码将放在一个名为backupToZip()的函数中。这样就更容易将该函数复制粘贴到其他需要这个功能的Python 程序中。在这个程序的末尾,会调用这个函数进行备份。

第1 步:弄清楚ZIP 文件的名称

通过检查delicious_1.zip 是否存在,然后检查delicious_2.zip 是否存在,继续下去,可以确定N 应该是什么。用一个名为number 的变量表示N,在一个循环内不断增加它,并调用os.path.exists()来检查该文件是否存在。第一个不存在的文件名将导致循环break,因此它就发现了新ZIP 文件的文件名。

#! python3
# 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 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')

第2 步:创建新ZIP 文件

	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')

第3 步:遍历目录树并添加到ZIP 文件

	# 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')

实现代码

#! python3
# 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 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
	# 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)
		# 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')

你可能感兴趣的:(Python)