Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. def iterateDirectories(thispath,maxim,fname):
  2.     saved_files=[0]
  3.     stop=False
  4.     # Saved files: Index start at 0 but file saving starts at 1
  5.     info_report = defaultdict(lambda: [])
  6.     ind=0
  7.     for subdir, dirs, files in os.walk(thispath):
  8.         if(stop==False):
  9.             found=False
  10.             #print("Scanning: " + subdir)
  11.             for file in files:
  12.                 file_name = file.partition('_')[0]
  13.                 #print os.path.join(subdir, file)
  14.                 filepath = subdir + os.sep + file
  15.  
  16.                 if filepath.endswith(".txt"):
  17.                     try:
  18.                         thisReport = processFile(file,subdir)
  19.                         found=True
  20.                     except MemoryError as err:
  21.                         print("MemoryError: ")
  22.                         dfs_in_memory()
  23.                         continue
  24.                     except:
  25.                         print("EXCEPTION AT ITERATE DIRS")
  26.                         continue
  27.                     if(not is_empty_frame(thisReport)):
  28.                         info_report[subdir].append(thisReport)
  29.  
  30.             #After traversal of one directory, join all dframes and save to persistent storage
  31.             #Then delete all DF's to save memory usage
  32.             #uid = saved_files[-1:] #Last element
  33.             if(found==True): #uid[0] != "False" and
  34.                 #uid = uid[0] + 1
  35.                 #dfs_in_memory()
  36.                 #saveall_df(info_report[subdir],uid) #Save and delete
  37.                 #saved_files.append(uid)
  38.                 ind+=1
  39.                 try:
  40.                     append_csv(info_report[subdir],fname)
  41.                 except:
  42.                     print("ERROR: Appending failed. One more attempt..")
  43.                     try:
  44.                         append_csv(info_report[subdir],fname)
  45.                     except:
  46.                         print("Error: Given up appending line.")
  47.                        
  48.                    
  49.                 info_report[subdir] = [] #Remove values.
  50.                 #print("File Uid: " + str(uid) + " saved.")
  51.                 print("Subdir: " + str(subdir))
  52.                 print(str(ind)+ " Files saved so far")
  53.                 if(ind==maxim):
  54.                     print("=========================END SCAN [Max_Bound] =========================")
  55.                     stop=True
  56.                     return True
  57.  
  58.                 #dfs_in_memory()
  59.             else:
  60.                 pass
  61.                
  62.     print("=========================END SCAN=========================")
  63.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement