how to temporarily rename a file or create a renamed temp-file in python before zipping it?

ZipFile.write has a second parameter, arcname, which allows you to rename files without any copying. You don’t need to move file to a separate folder, or actually rename it.

from os.path import basename

for file in filenames_list:
    if (name := get_friendly_name(file)) is None:
        name = basename(file)
zipF.write(file, name, compress_type=zipfile.ZIP_DEFLATED)

By stripping off the basename, you avoid the need to move to a common folder at all.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top