简体   繁体   中英

How to add a password and output directory using Zipfile module in Python?

I got below code from online and I am trying to add a password and I want to change the result directory to be "C:#SFTPDWN" (Final Zip file should be in this folder).

I try to change it like below, it did not work.

with ZipFile('CC-Data.zip', 'w', 'pass word') as zip:

Can anybody please tell how to change this code to add password and change result folder?

One last thing, currently it will zip #SFTPDWN folder, I just want to zip everything inside (Right now it will create two folders (CC-Data.zip and inside it #SFTPDWN )). Can anybody please tell me how to zip everything inside #SFTPDWN folder?

Code

from zipfile import ZipFile
import os


def get_all_file_paths(directory):
    file_paths = []

    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)

    return file_paths


def main():
    # path to folder which needs to be zipped
    directory = 'C:\#SFTPDWN'

    file_paths = get_all_file_paths(directory)

    print('Following files will be zipped:')
    for file_name in file_paths:
        print(file_name)

    with ZipFile('CC-Data.zip', 'w') as zip:
        # writing each file one by one
        for file in file_paths:
            zip.write(file)

    print('Zipped successfully!')


if __name__ == "__main__":
    main()

For the password question: from the documentation:

This module [...] supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file . Decryption is extremely slow as it is implemented in native Python rather than C.

You would need to use a 3rd party library to create an encrypted zip, or encrypt the archive some other way.

For the second part, in ZipFile.write the documentation also mentions:

ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None)

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). [...]

Note: Archive names should be relative to the archive root, that is, they should not start with a path separator.

So you would need to strip off whatever prefix of your file variable and pass that as the arcname parameter. Using os.path.relpath may help, eg (I'm on Linux, but should work with Windows paths under Windows):

>>> os.path.relpath("/folder/subpath/myfile.txt", "/folder/")
'subpath/myfile.txt'

Sidebar: a path like "C:\\Something" is an illegal string, as it has the escape \\S . Python kinda tolerates this (I think in 3.8 it will error) and rewrites them literally. Either use "C:\\\\Something" , r"C:\\Something" , or "C:/Something" If you attempted something like "C:\\Users" it would actually throw an error, or "C:\\nothing" it might silently do something strange...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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