简体   繁体   中英

Convert file names inside ZIP archive in Python

Is it possible to rename files inside folders of a zip archive using python? I have this zip downloaded from here , but file names contain ::, so Windows doesn't like it. Simple .replace('::','-') on it would make my day

just loop through the archive and write the files out as whatever you want them to be called

    with zipfile.ZipFile(filename, "r") as zf:
        extracted_names = zf.namelist()
        for name in extracted_names:
            new_filename = name.replace('::', '')
            with open(new_filename, 'wb') as f:
                f.write(zf.read(name))

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