压缩文件夹及其内容

时间:2019-08-19 14:55:52

标签: python zipfile

我想压缩文件夹“ com.facebook.katana”及其内容。目录结构如下:

    AndroidBags
    Downloads
     ├── com.facebook.katana
     │     ├── one.txt
     │     └── two.txt
     │     ├── Data
     │     └── PetController.java

我想将此.zip文件创建到./AndroidBags/中,这是我的压缩文件的代码:

def retrieve_file_paths(dirName):
    # setup file paths variable
    filePaths = []

    # Read all directory, subdirectories and file lists
    for root, directories, files in os.walk(dirName):
        for filename in files:
            # Create the full filepath by using os module.
            filePath = os.path.join(root, filename)
            filePaths.append(filePath)

    # return all paths
    return filePaths


# Declare the main function
def main():
    # Assign the name of the directory to zip
    dir_name = 'mydir'

    # Call the function to retrieve all files and folders of the assigned directory
    filePaths = retrieve_file_paths(dir_name)

    # printing the list of all files to be zipped
    print('The following list of files will be zipped:')
    for fileName in filePaths:
        print(fileName)

    # writing files to a zipfile
    zip_file = zipfile.ZipFile(dir_name + '.zip', 'w')
    with zip_file:
        # writing each file one by one
        for file in filePaths:
            zip_file.write(file)

    print(dir_name + '.zip file is created successfully!')

我的问题是,当我创建zip文件时,我创建了一个'./Downloads'文件夹。当我将其解压缩到AndroidBags文件夹中时,如何删除它以直接具有“ com.facebook.katana”及其包含的文件?

谢谢

0 个答案:

没有答案