Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import zipfile
- import rarfile
- import argparse
- from os import listdir, walk, sep
- from os.path import isfile, join
- def checkFileEnding(fileName, filePath):
- if fileName.lower().endswith(tuple(args.extension)):
- try:
- splittedFilePath = filePath.split('\\') # Split on \ so we can remove the .rar file and enter a new one
- splittedFilePath.pop() # Remove the filename
- if '/' in fileName:
- fileName = fileName.split('/').pop()
- completePath = join('\\'.join(splittedFilePath), fileName) # Join the real path with the actual file name
- open(completePath, "x")
- except FileExistsError:
- print('File already exists: ' + completePath + ' found inside: ' + filePath)
- def main():
- if args.diveMap == True:
- onlyfiles = [join(root, file) for root, dirs, files in walk(args.path) for file in files]
- else:
- onlyfiles = [args.path + file for file in listdir(args.path) if isfile(join(args.path, file))]
- for file in onlyfiles:
- if file.endswith('.zip'):
- files = zipfile.ZipFile(file, 'r')
- elif file.endswith('.rar'):
- files = rarfile.RarFile(file)
- else:
- continue
- for item in files.infolist():
- # Do stupid error handling cause rarfile and zipfile diff... is_dir() vs isdir()
- try:
- if item.is_dir(): continue
- except AttributeError:
- if item.isdir(): continue
- checkFileEnding(item.filename, file)
- parser = argparse.ArgumentParser(description='Search for filetype in ZIP/RAR and create dummy files outside it')
- parser.add_argument('--ext', help='File extension to search for, example: .OBJ', dest='extension', type=str.lower, default=['.obj'], nargs='*')
- parser.add_argument('--path', help='path to directory to search in, example: C:\\Users\\OscarAndersson\\Desktop\\script', dest='path', required=True)
- parser.add_argument('--diveMap', help='To search for files deeper the the input directory', dest='diveMap', const=True, default=False, nargs='?')
- args = parser.parse_args()
- if args.extension[0] == 'q':
- args.extension = input('Please enter extension(s): ').split(' ')
- if not args.path.endswith('\\'):
- args.path = args.path + '\\'
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement