How to add multiple files to one zip folder

Actually, I am writing a script that writes two files to the desktop, let it be "a.txt" and "b.txt" ....... so after writing to the desktop I need to read these files and write to the folder ....

can someone help with this .... i know how to zip up a folder but dono how to add two files to zip

Reading from a folder I know how it looks

def zipdir(basedir, archivename):
    assert os.path.isdir(basedir)
    with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
        for root, dirs, files in os.walk(basedir):
            for fn in files:
                absfn = os.path.join(root, fn)
                zfn = absfn[len(basedir)+len(os.sep):]
                z.write(absfn, zfn)

if __name__ == '__main__':
    import sys
    basedir = sys.argv[1]
    archivename = sys.argv[2]
    zipdir(basedir, archivename)

The code I'm using now

import zipfile
zip = zipfile.ZipFile('Python.zip', 'a')
zip.write('fields.txt')
zip.write('grp.txt')
zip.close()

This creates a file of these two plus an additional folder containing all the files ........

+3
source share
1 answer

you need to open the zip file with the parameter "a" -append. Then you can use the write option without overwriting the file.

source: 12.4.1

EDIT:

zip.write ('file.pdf', '/folder/file.pdf')

+1
source

All Articles