Advertisement
Guest User

Untitled

a guest
Feb 15th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import zipfile
  4. import rarfile
  5. import argparse
  6. from os import listdir, walk, sep
  7. from os.path import isfile, join
  8.  
  9.  
  10. def checkFileEnding(fileName, filePath):
  11.   if fileName.lower().endswith(tuple(args.extension)):
  12.     try:
  13.       splittedFilePath = filePath.split('\\') # Split on \ so we can remove the .rar file and enter a new one
  14.       splittedFilePath.pop() # Remove the filename
  15.       if '/' in fileName:
  16.         fileName = fileName.split('/').pop()
  17.       completePath = join('\\'.join(splittedFilePath), fileName) # Join the real path with the actual file name
  18.       open(completePath, "x")
  19.     except FileExistsError:
  20.       print('File already exists: ' + completePath + ' found inside: ' + filePath)
  21.  
  22. def main():
  23.   if args.diveMap == True:
  24.     onlyfiles = [join(root, file) for root, dirs, files in walk(args.path) for file in files]
  25.   else:
  26.     onlyfiles = [args.path + file for file in listdir(args.path) if isfile(join(args.path, file))]
  27.   for file in onlyfiles:
  28.     if file.endswith('.zip'):
  29.       files = zipfile.ZipFile(file, 'r')
  30.     elif file.endswith('.rar'):
  31.       files = rarfile.RarFile(file)
  32.     else:
  33.       continue
  34.     for item in files.infolist():
  35.       # Do stupid error handling cause rarfile and zipfile diff... is_dir() vs isdir()
  36.       try:
  37.         if item.is_dir(): continue
  38.       except AttributeError:
  39.         if item.isdir(): continue
  40.       checkFileEnding(item.filename, file)
  41.  
  42. parser = argparse.ArgumentParser(description='Search for filetype in ZIP/RAR and create dummy files outside it')
  43. parser.add_argument('--ext', help='File extension to search for, example: .OBJ', dest='extension', type=str.lower, default=['.obj'], nargs='*')
  44. parser.add_argument('--path', help='path to directory to search in, example:  C:\\Users\\OscarAndersson\\Desktop\\script', dest='path', required=True)
  45. parser.add_argument('--diveMap', help='To search for files deeper the the input directory', dest='diveMap', const=True, default=False, nargs='?')  
  46. args = parser.parse_args()
  47.  
  48. if args.extension[0] == 'q':
  49.   args.extension = input('Please enter extension(s): ').split(' ')
  50.  
  51. if not args.path.endswith('\\'):
  52.   args.path = args.path + '\\'
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement