-3

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!

2 Answers2

1

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')
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
1

'

You have a couple of ways in Python to get a list of files. The easiest is probably to use the 'glob' module, like this:

import glob
filelist = glob.glob('/home/tryeverylanguage/mprm*')

For details on glob, start an interactive Python session by typing 'python' at the command prompt and then typing import glob, then help(glob)

To get the files you list into a zip file, you'll probably want to use the 'zipfile' module. For details on this module, in your Python interactive session type import zipfile and then help(zipfile)

By the way, don't confuse the Python built-in 'zip' function with the command to create zip files -- it's completely unrelated.

Hopefully, this will be enough to get you started. Have fun with Python!