简体   繁体   English

Python-没有父文件夹的Zipfile

[英]Python - Zipfile with no parent folder

I am able to compress files into a single zip file, however when I open the zip file the contents always include a "parent folder" which has the same exact name as the zip file itslef. 我可以将文件压缩为单个zip文件,但是当我打开zip文件时,内容始终包含一个“父文件夹”,该名称与zip文件itslef的名称相同。

I'm simply trying to create a zip file that contains no folders at all. 我只是试图创建一个不包含任何文件夹的zip文件。

Current: abc.zip > abc (folder) > files 当前:abc.zip> abc(文件夹)>文件

Looking for: abc.zip > files 寻找:abc.zip>文件

def create_zip(file_name, file_dir):
    """
    Function to create zip archive
    """
    os.chdir(file_dir)
    zipfiles_list = ['csv', 'txt']
    file_name_part = file_name
    zip_name = '{0}.zip'.format(file_name_part)
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as 
    zip_archive:
        for file_extension in zipfile_list:
            full_file_path = '{0}.{1}'.format(file_name_part, file_extension)
            zip_archive.write(full_file_path, basename(full_file_path))

Is this possible? 这可能吗? I've seen that if I rename the zip file after it's created, it will still extract contents into a folder that matches the zip file name. 我已经看到,如果在创建zip文件后重命名它,它仍会将内容提取到与zip文件名匹配的文件夹中。

Original zip extracts to abc folder 原始zip解压缩到abc文件夹

Renamed same exact zip and contents extract to folder with new name 重命名相同的zip,并将内容提取到具有新名称的文件夹

In your code you have a mistake in a word ' zipfiles ': for file_extension in zipfile_list: (without 's' in the end). 在您的代码中,您for file_extension in zipfile_list:了“ zipfiles ”:错误(最后没有“ s”)。

It have to be for file_extension in zipfiles_list: . 它必须是for file_extension in zipfiles_list:

Than I run your code in Win7x86 - it is OK, no inner folder (python 3.6.6). 比起我在Win7x86中运行您的代码-可以,没有内部文件夹(python 3.6.6)。

Tried it on Win 10x64 - same. 在Win 10x64上尝试过-相同。

Xubuntu 18.04 x64 - same, without folder. Xubuntu 18.04 x64-相同,没有文件夹。

Here is my script: 这是我的脚本:

from os.path import basename 
from os import path
import os
import zipfile

# current directory
script_dir = path.dirname(path.abspath(__file__))

def create_zip(file_name, file_dir):
    """
    Function to create zip archive
    """
    os.chdir(file_dir)
    zipfiles_list = ['csv', 'txt']
    file_name_part = file_name
    zip_name = '{0}.zip'.format(file_name_part)
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zip_archive:
        for file_extension in zipfiles_list:
            full_file_path = '{0}.{1}'.format(file_name_part, file_extension)
            zip_archive.write(full_file_path, basename(full_file_path))

create_zip("first", script_dir)

In a directory with script there two files: first.txt and first.csv. 在包含脚本的目录中,有两个文件:first.txt和first.csv。 After runing this scrip I do have zip file first.zip with two files inside: first.txt and first.csv. 运行此脚本后,我确实拥有一个zip文件first.zip,其中包含两个文件:first.txt和first.csv。

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

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