Case study: Read text information from a mailbox and generate new file information for saving
2018-10-23
BUG Experience: You can't open a file twice in a file as a file object with the same name, such as file_obj and file_obj2 in line 14 and 36 below, respectively.
1. Processing the file formatting under the following file structure into another file structure to save
#__author:"Luck*good" #date: 2018/10/23 0023 #function: # BUG Experience: Opening a file twice in a file can't as File objects with the same name, such as lines 14 and 36 below, have different names file_obj and file_obj2 # Read text information from a mailbox and generate new file information for saving import os import collections # Processing ordinary file functions def work(filePath): resultDir = r'E:\[AAA](thousand)Full stack learning python\18-10-21\day7\resultDir' with open(filePath,'r') as file_obj: while True: #Read the first row of data such as #laphael1985@163.com----198587 lineInfo = file_obj.readline() # Set an exit while Signs of Circulation if len(lineInfo) < 5: break mailPsw = lineInfo.split('----')[-1] mailStr = lineInfo.split('----')[0] mailType = mailStr.split('@')[-1].split('.')[0] mailDetail = mailStr.split('@')[0] #Store as new folders and file text operations newDirPath = os.path.join(resultDir,mailType) # Check to create a folder without it if not os.path.exists(newDirPath): os.mkdir(newDirPath) #After creating the type folder, store the new file information in it newFilePath = os.path.join(newDirPath,mailType+'.txt') with open(newFilePath,'a') as file_obj2: file_obj2.write(mailDetail) file_obj2.write('----') file_obj2.write(mailPsw+'\n') def getAllDirIT(path): queue=collections.deque() #Enter the team queue.append(path) #When the queue is empty, stop the loop while len(queue) != 0: #Out of the team data here is equivalent to finding A Absolute path of element dirPath = queue.popleft() # Find out all subdirectory information under the directory, or file information under the directory. dirList = os.listdir(dirPath) #Traverse through other information under this folder for filename in dirList: #Absolute Path dirAbsPath = os.path.join(dirPath,filename) # Judgment: If it is a catalogue dir Entry operation, if not dir Print it out. if os.path.isdir(dirAbsPath): print("Catalog:"+filename) queue.append(dirAbsPath) else: # Find common files and process information. Pass the path of this ordinary file through work(dirAbsPath) # Call of function getAllDirIT(r'E:\[AAA](thousand)Full stack learning python\18-10-21\day7\newdir')
Result:
Password saved after 163 mailbox processing