Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- # fileTypeZipper.py - function arguments are a filetype, directory and optional zip file name
- import os, re, zipfile
- def zipFileType(filetype, dir='', zipname=''):
- #Filetype is required. For example user enters ZIP. we must add the '.' to make this '.ZIP'
- filetypeName = filetype
- filetype = '.' + filetype
- #TODO: fix this regex so that it contains the preceding characters.
- filetypeRegex = re.compile('' + str(filetype) + '$')
- #If dir is not defined then it will be the folder in which this program is ran
- if dir == None:
- dir = os.getcwd()
- else:
- dir = os.path.abspath(dir)
- print('directory:' + dir)
- #If zipname is not defined then it will be directory plus filetype
- if zipname == '':
- zipname = str(os.path.basename(os.path.dirname(dir)) + filetype)
- returnZip = zipfile.ZipFile(zipname, 'w')
- #For files in directory - if ext = filetype then add to the zip
- for file in os.listdir(dir):
- #print(file)
- if re.findall(filetypeRegex, file) != []:
- returnZip.write(file)
- print('zipping : ' + file)
- print(zipname + ' has been zipped . ')
- returnZip.close()
- zipFileType('zip')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement