Python 将多个子目录压缩到单个拉链的路径
我需要帮助编辑下面的脚本拉链目录的内容。我的最终目标是创建一个脚本,该脚本将查看C:\Test(其中包含多个目录),并使用C:\Test中每个目录的内容创建一个新的zip文件。棘手的是,我需要的路径是C:\即使目录的真实路径是C:\ Test。这是可能的还是我在做梦 谢谢Python 将多个子目录压缩到单个拉链的路径,python,zip,winzip,Python,Zip,Winzip,我需要帮助编辑下面的脚本拉链目录的内容。我的最终目标是创建一个脚本,该脚本将查看C:\Test(其中包含多个目录),并使用C:\Test中每个目录的内容创建一个新的zip文件。棘手的是,我需要的路径是C:\即使目录的真实路径是C:\ Test。这是可能的还是我在做梦 谢谢 import zipfile, os def makeArchive(fileList, archive): try: a = zipfile.ZipFile(archive, 'w', zipfi
import zipfile, os
def makeArchive(fileList, archive):
try:
a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
for f in fileList:
print "archiving file %s" % (f)
a.write(f)
a.close()
return True
except: return False
def dirEntries(dir_name, subdir, *args):
fileList = []
for file in os.listdir(dir_name):
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
if not args:
fileList.append(dirfile)
else:
if os.path.splitext(dirfile)[1][1:] in args:
fileList.append(dirfile)
# recursively access file names in subdirectories
elif os.path.isdir(dirfile) and subdir:
print "Accessing directory:", dirfile
fileList.extend(dirEntries(dirfile, subdir, *args))
return fileList
if __name__ == '__main__':
folder = r'C:\test'
zipname = r'C:\test\test.zip'
makeArchive(dirEntries(folder, True), zipname)
您可以按如下方式更改存档中文件的路径:
a.write(PATH_ON_FILESYSTEM,
DESIRED_PATH_IN_ARCHIVE
)
我似乎无法改变路径,但我稍后会尝试。更重要的是,我需要scipt为每个目录创建多个ZIP文件。目前,脚本使用每个目录制作一个大ZIP。