简体   繁体   English

Zip 文件并保存到特定文件夹

[英]Zip file and save to specific folder

this might be a silly question but I'm struggling a lot finding solution to it.这可能是一个愚蠢的问题,但我正在努力寻找解决方案。

So I have a file in the given folder:所以我在给定文件夹中有一个文件:

Output\20190101_0100\20190101_0100.csv

Now I want to zip the file and save it to same location.现在我想 zip 文件并将其保存到同一位置。 So here's my try:所以这是我的尝试:

zipfile.ZipFile('Output/20190101_0100/20190101_0100_11.zip', mode='w', compression=zipfile.ZIP_DEFLATED).write('Output/20190101_0100/20190101_0100_11.csv')

But it's making a folder insider zip folder and saving it, as shown below:但它是在 zip 文件夹内创建一个文件夹并保存,如下图:

Output\20190101_0100\20190101_0100_11.zip\Output\20190101_0100\20190101_0100_11.csv

Can someone tell me how can I save my file directly in the same location or location mentioned below:有人可以告诉我如何将我的文件直接保存在下面提到的相同位置或位置:

Output\20190101_0100\20190101_0100_11.zip\20190101_0100_11.csv

Rephrasing of question问题的改写

The question is slightly confusing because Output\20190101_0100\20190101_0100_11.zip\Output\20190101_0100\20190101_0100_11.csv won't be a file, but rather Output\20190101_0100\20190101_0100_11.csv will be a file within the zip file Output\20190101_0100\20190101_0100_11.zip (if I am not mistaken) The question is slightly confusing because Output\20190101_0100\20190101_0100_11.zip\Output\20190101_0100\20190101_0100_11.csv won't be a file, but rather Output\20190101_0100\20190101_0100_11.csv will be a file within the zip file Output\20190101_0100\20190101_0100_11.zip (如果我没记错的话)

Just to restate your problem (if I understood it correctly):只是为了重申您的问题(如果我理解正确的话):

  • You have a file Output\20190101_0100\20190101_0100.csv (a file 20190101_0100.csv in the Output -> 20190101_0100 sub directory) You have a file Output\20190101_0100\20190101_0100.csv (a file 20190101_0100.csv in the Output -> 20190101_0100 sub directory)
  • You want to create the zip file Output/20190101_0100/20190101_0100_11.zip ( 20190101_0100_11.zip in the Output -> 20190101_0100.zip directory) You want to create the zip file Output/20190101_0100/20190101_0100_11.zip ( 20190101_0100_11.zip in the Output -> 20190101_0100.zip directory)
  • You want to add the aforementioned CSV file Output\20190101_0100\20190101_0100.csv but without the leading path, ie as 20190101_0100_11.csv rather than Output\20190101_0100\20190101_0100.csv . You want to add the aforementioned CSV file Output\20190101_0100\20190101_0100.csv but without the leading path, ie as 20190101_0100_11.csv rather than Output\20190101_0100\20190101_0100.csv .

Or to not get confused with too many similar directories, let's simplify it as:或者为了不与太多类似的目录混淆,让我们将其简化为:

  • You have a file test.csv in the sub directory sub-folder您在子目录sub-folder中有一个文件test.csv
  • You want to create the zip file test.zip您要创建 zip 文件test.zip
  • You want to add the aforementioned CSV file test.csv but without the leading path, ie as test.csv rather than sub-folder/test.csv . You want to add the aforementioned CSV file test.csv but without the leading path, ie as test.csv rather than sub-folder/test.csv .

Answer回答

From the ZipFile.write documentation:ZipFile.write文档中:

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename , but without a drive letter and with leading path separators removed).将名为filename的文件写入存档,并为其指定存档名称arcname (默认情况下,这将与filename相同,但没有驱动器号并删除了前导路径分隔符)。

That means that arcname will default to the passed in filename (it doesn't have a drive letter or leading path separator).这意味着arcname将默认为传入的filename (它没有驱动器号或前导路径分隔符)。

If you want to remove the sub folder part, just pass in arcname as well.如果要删除子文件夹部分,只需传入arcname即可。 eg:例如:

import zipfile

with zipfile.ZipFile('path-to-zip/test.zip', 'w') as zf:
    zf.write('sub-folder/test.csv', arcname='test.csv')

You could try using a raw path:您可以尝试使用原始路径:

zipfile.ZipFile('Output/20190101_0100/20190101_0100_11.zip', mode='w', compression=zipfile.ZIP_DEFLATED).write(r'C:\...\Output\20190101_0100\20190101_0100_11.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM