Search code examples
pythonbashzipphp-ziparchive

Zip only files starting with mprm* into mprm.zip in python


I am trying to write a code to zip files starting with mprm* to mprm.zip file. My directory has 100 different files with all different types of extension.

As in Bash we can do

zip -r pathtofile/mprm path2Destination/mprm*

is there something in python that can achieve same?

Thanks!


Solution

  • Something like this should work:

    (worked on python34)

    import glob
    from zipfile import ZipFile, ZIP_DEFLATED
    
    files = glob.glob('mprm*') #find your stuff
    z = ZipFile('mprm.zip', mode='w', compression=ZIP_DEFLATED) #change if needed
    for f in files:
        z.write(f, f)
    print ('done')