Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def unzip(source_filename, dest_dir):
- output_list=[] #Make a list for output dirs
- with zipfile.ZipFile(source_filename) as zf:
- for member in zf.infolist():
- words = member.filename.split('/') ###############################
- path = dest_dir ### ###
- for word in words[:-1]: ### ###
- drive, word = os.path.splitdrive(word) ### ###
- head, word = os.path.split(word) ###Consider rewriting this ###
- if word in (os.curdir, os.pardir, ''): continue ### ###
- path = os.path.join(path, word) ###############################
- zf.extract(member, path)
- absolute_path=#the_absolute_path_of_the_extracted_file
- output_list.append(absolute_path)
- return output_list #Return list of what should be ABSOLUTE paths
- current_dir=#args
- output_path=#args
- #You should also check if these are valid paths with os.path.isdir
- for root,dirs,files in os.walk(current_dir):
- for f in files:
- if f.lower().endswith('.zip'): #formatted to .lower to eleminate case inconsistencies
- file_path=os.path.join(root,f)
- out_files=unzip(file_path,root) #list of files extracted
- for uf in out_files: #I'm assuming you want to move all the files moved out from the zip, not the zip itself, which if the variable f
- output_file_path=os.path.join(output_path,uf)
- if not os.path.exists(output_file_path): #If it doesn't exist, we can move it there.
- shutil.move(uf,output_file_path)
- else: #The path existed and we have to try a different one.
- n=2
- while True:
- new_file_ending="_"+str(n)+".gbd" #assuming you want it to look like "filename_2.gbd"
- new_out_fp=output_file_path.lower.replace('.gbd',new_file_ending) #formatted to lowercase to ensure replace of .gbd works
- if not os.path.exists(new_out_fp):
- shutil.move(uf, new_out_fp)
- else:
- n+=1
- continue #in the while True loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement