Advertisement
Guest User

Sample_Code

a guest
Feb 11th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. def unzip(source_filename, dest_dir):
  2.     output_list=[] #Make a list for output dirs
  3.     with zipfile.ZipFile(source_filename) as zf:
  4.         for member in zf.infolist():
  5.             words = member.filename.split('/')                  ###############################
  6.             path = dest_dir                                     ###                         ###
  7.             for word in words[:-1]:                             ###                         ###
  8.                 drive, word = os.path.splitdrive(word)          ###                         ###
  9.                 head, word = os.path.split(word)                ###Consider rewriting this  ###
  10.                 if word in (os.curdir, os.pardir, ''): continue ###                         ###
  11.                 path = os.path.join(path, word)                 ###############################
  12.             zf.extract(member, path)
  13.             absolute_path=#the_absolute_path_of_the_extracted_file
  14.             output_list.append(absolute_path)
  15.     return output_list #Return list of what should be ABSOLUTE paths
  16.  
  17.  
  18.  
  19.  
  20. current_dir=#args
  21. output_path=#args
  22.  
  23. #You should also check if these are valid paths with os.path.isdir
  24.  
  25. for root,dirs,files in os.walk(current_dir):
  26.     for f in files:
  27.         if f.lower().endswith('.zip'): #formatted to .lower to eleminate case inconsistencies
  28.             file_path=os.path.join(root,f)
  29.             out_files=unzip(file_path,root) #list of files extracted
  30.             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
  31.                 output_file_path=os.path.join(output_path,uf)
  32.                 if not os.path.exists(output_file_path): #If it doesn't exist, we can move it there.
  33.                     shutil.move(uf,output_file_path)
  34.                 else: #The path existed and we have to try a different one.
  35.                     n=2
  36.                     while True:
  37.                             new_file_ending="_"+str(n)+".gbd" #assuming you want it to look like "filename_2.gbd"
  38.                             new_out_fp=output_file_path.lower.replace('.gbd',new_file_ending) #formatted to lowercase to ensure replace of .gbd works
  39.                             if not os.path.exists(new_out_fp):
  40.                                 shutil.move(uf, new_out_fp)
  41.                             else:
  42.                                 n+=1
  43.                                 continue #in the while True loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement