Python zipfile.write():文件的相对路径,在zip存档中复制
使用zip文件,我指示文件位于其他文件夹中,例如:Python zipfile.write():文件的相对路径,在zip存档中复制,python,path,zipfile,Python,Path,Zipfile,使用zip文件,我指示文件位于其他文件夹中,例如:”./data/2003-2007/metropolis/Matrix\u 0\u 1\u 0.csv' 我的问题是,当我提取它时,文件位于/data/2003-2007/metropolis/Matrix\u 0\u 1\u 0.csv,而我希望它在/中提取 这是我的密码: def zip_files(src, dst): zip_ = zipfile.ZipFile(dst, 'w') print src, dst
”./data/2003-2007/metropolis/Matrix\u 0\u 1\u 0.csv'
我的问题是,当我提取它时,文件位于/data/2003-2007/metropolis/Matrix\u 0\u 1\u 0.csv
,而我希望它在/
中提取
这是我的密码:
def zip_files(src, dst):
zip_ = zipfile.ZipFile(dst, 'w')
print src, dst
for src_ in src:
zip_.write(src_, os.path.relpath(src_, './'), compress_type = zipfile.ZIP_DEFLATED)
zip_.close()
以下是src和dst的打印:
['./data/2003-2007/metropolis/Matrix_0_1_0.csv', './data/2003-2007/metropolis/Matrix_0_1_1.csv'] ./data/2003-2007/metropolis/csv.zip
如图所示:
解决办法是:
'''
zip_file:
@src: Iterable object containing one or more element
@dst: filename (path/filename if needed)
@arcname: Iterable object containing the names we want to give to the elements in the archive (has to correspond to src)
'''
def zip_files(src, dst, arcname=None):
zip_ = zipfile.ZipFile(dst, 'w')
print src, dst
for i in range(len(src)):
if arcname is None:
zip_.write(src[i], os.path.basename(src[i]), compress_type = zipfile.ZIP_DEFLATED)
else:
zip_.write(src[i], arcname[i], compress_type = zipfile.ZIP_DEFLATED)
zip_.close()
在这种情况下,最好使用
tarfile
:
with tarfile.open(output, "w:gz") as tar:
# if we do not provide arcname, archive will include full paths
arcname = path.split('/')[-1]
tar.add(path, arcname)
tar.close()
导入操作系统
进口拉链
def zipdir(src、dst、邮政编码):
"""
函数从dst位置的src创建zip存档。存档的名称为zip_name。
:param src:要存档的目录的路径。
:param dst:将存储存档目录的路径。
:param zip_name:存档的名称。
:返回:无
"""
###目标目录
os.chdir(dst)
###拉链处理器
ziph=zipfile.zipfile(zip_名称'w')
###将src目录的内容写入存档
对于os.walk(src)中的根目录、目录和文件:
对于文件中的文件:
###在这种情况下,zip存档的结构将是:
###C:\Users\BO\Desktop\20200307.zip\Audacity\
#write(os.path.join(root,文件),arcname=os.path.join(root.replace(os.path.split(src)[0],“”),文件))
###在这种情况下,zip存档的结构将是:
###C:\Users\BO\Desktop\20200307.zip\
write(os.path.join(root,文件),arcname=os.path.join(root.replace(src,“”,文件))
ziph.close()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
zipdir(“C:/Users/BO/Documents/Audacity”、“C:/Users/BO/Desktop”、“20200307.zip”)
请参见“谢谢”,但它似乎没有链接。他想抽,我想压缩。他不使用ZipFile.write()进行写入,而不保留目录结构,请参见。我已经读过了,但我没有弄清楚
import os
import zipfile
def zipdir(src, dst, zip_name):
"""
Function creates zip archive from src in dst location. The name of archive is zip_name.
:param src: Path to directory to be archived.
:param dst: Path where archived dir will be stored.
:param zip_name: The name of the archive.
:return: None
"""
### destination directory
os.chdir(dst)
### zipfile handler
ziph = zipfile.ZipFile(zip_name, 'w')
### writing content of src directory to the archive
for root, dirs, files in os.walk(src):
for file in files:
### In this case the structure of zip archive will be:
### C:\Users\BO\Desktop\20200307.zip\Audacity\<content of Audacity dir>
# ziph.write(os.path.join(root, file), arcname=os.path.join(root.replace(os.path.split(src)[0], ""), file))
### In this case the structure of zip archive will be:
### C:\Users\BO\Desktop\20200307.zip\<content of Audacity dir>
ziph.write(os.path.join(root, file), arcname=os.path.join(root.replace(src, ""), file))
ziph.close()
if __name__ == '__main__':
zipdir("C:/Users/BO/Documents/Audacity", "C:/Users/BO/Desktop", "20200307.zip")