Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #! python3
  2. # fileTypeZipper.py - function arguments are a filetype, directory and optional zip file name
  3.  
  4. import os, re, zipfile
  5.  
  6.  
  7. def zipFileType(filetype, dir='', zipname=''):
  8.     #Filetype is required. For example user enters ZIP. we must add the '.' to make this '.ZIP'
  9.     filetypeName = filetype
  10.     filetype = '.' + filetype
  11.     #TODO: fix this regex so that it contains the preceding characters.
  12.     filetypeRegex = re.compile('' + str(filetype) + '$')
  13.  
  14.     #If dir is not defined then it will be the folder in which this program is ran
  15.     if dir == None:
  16.         dir = os.getcwd()
  17.     else:
  18.         dir = os.path.abspath(dir)
  19.  
  20.     print('directory:' + dir)
  21.  
  22.     #If zipname is not defined then it will be directory plus filetype
  23.     if zipname == '':
  24.         zipname = str(os.path.basename(os.path.dirname(dir)) + filetype)
  25.  
  26.     returnZip = zipfile.ZipFile(zipname, 'w')
  27.  
  28.     #For files in directory - if ext = filetype then add to the zip
  29.     for file in os.listdir(dir):
  30.         #print(file)
  31.         if re.findall(filetypeRegex, file) != []:
  32.             returnZip.write(file)
  33.             print('zipping : ' + file)
  34.  
  35.     print(zipname + ' has been zipped . ')
  36.     returnZip.close()
  37.  
  38. zipFileType('zip')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement