用Python将文件夹打包成Zip并备份至U盘

编程

放码过来

python">import os

import zipfile

import datetime

import shutil

nowTimeStr = datetime.datetime.now().strftime("%Y%m%d%H%M")

newZipFileName = "nice%s.zip" % nowTimeStr

newZip = zipfile.ZipFile(newZipFileName, "w")

# pack the src folder

for folderName, subfolders, filenames in os.walk("."):

if folderName.startswith(".\src"):

for filename in filenames:

path = "%s\%s"%(folderName, filename)

print path

newZip.write(path, compress_type=zipfile.ZIP_DEFLATED)

if folderName == ".":

for filename in filenames:

if "pom.xml" == filename or "backup.py" == filename:

path = "%s\%s"%(folderName, filename)

print path

newZip.write(path, compress_type=zipfile.ZIP_DEFLATED)

newZip.close()

#move zip to destination

backupPath = "E:\Backup"

if not os.path.exists(backupPath):

print "BackupPath: %s is not existed."%backupPath

else:

shutil.move(newZipFileName, backupPath)

print "Done"

raw_input()

参考资料

Python编程快速上手

以上是 用Python将文件夹打包成Zip并备份至U盘 的全部内容, 来源链接: utcz.com/z/514356.html

回到顶部