简体   繁体   中英

Zip select files from root directory maintaining the directory structure

I have a list of files written to a text file using command line(extracted list of files with a specific extension). I want to write these files to a zip file but not all files to a root directory but maintain the directory structure.

Ok, I was able to achieve this using the code below.

import os
import zipfile

f = open("notebooks.txt", "r")
lines =  f.readlines()
lines
with zipfile.ZipFile('file.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    for line in lines:
        line = line.rsplit("\n",1)[0]
        print(os.path.isfile(line))
        try:
            zipf.write(line)
            print(line,"passed")
        except Exception as e:
            print(e)
            pass

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