Advertisement
Guest User

Untitled

a guest
Feb 10th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 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.endswith(args.extension.lower()) or fileName.endswith(args.extension.upper()):
  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.       completePath = join('\\'.join(splittedFilePath), fileName) # Join the real path with the actual file name
  16.       open(completePath, "x")
  17.     except FileExistsError:
  18.       print('File already exists: ' + completePath + ' found inside: ' + filePath)
  19.  
  20. def main():
  21.   if args.diveMap == True:
  22.     onlyfiles = [join(root, file) for root, dirs, files in walk(args.path) for file in files]
  23.   else:
  24.     onlyfiles = [args.path + file for file in listdir(args.path) if isfile(join(args.path, file))]
  25.   for file in onlyfiles:
  26.     if file.endswith('.zip'):
  27.       files = zipfile.ZipFile(file, 'r')
  28.     elif file.endswith('.rar'):
  29.       files = rarfile.RarFile(file)
  30.     else:
  31.       continue
  32.     for item in files.infolist():
  33.       checkFileEnding(item.filename, file)
  34.  
  35. parser = argparse.ArgumentParser(description='Search for filetype in ZIP/RAR and create dummy files outside it')
  36. parser.add_argument('--ext', help='File extension to search for, example: .OBJ', dest='extension', default='.OBJ')
  37. parser.add_argument('--path', help='path to directory to search in, example:  C:\\Users\\OscarAndersson\\Desktop\\script', dest='path', required=True)
  38. parser.add_argument('--diveMap', help='To search for files deeper the the input directory', dest='diveMap', const=True, default=False, nargs='?')  
  39. args = parser.parse_args()
  40.  
  41. if not args.path.endswith('\\'):
  42.   args.path = args.path + '\\'
  43. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement