Case: Read the text message of a mailbox and generate new file information to save
2018-10-23
Bug tips: In a file two times open file cannot be the same name as the file object, such as the bottom of the 14 and 36 lines are different names File_obj and File_obj2
One, the following file structure under the file format processing for another file structure to save
#__author: "Kat * Jia"#DATE:2018/10/23 0023#function:#Bug tips: In a file two times open file cannot be the same name as the file object, such as the bottom of the 14 and 36 lines are different names File_obj and File_obj2#read text messages from a mailbox and generate new file information to saveImportOSImportCollections#working with normal file functionsdefWork (FilePath): Resultdir= R'e:\[aaa] (Thousand) Full stack learning Python\18-10-21\day7\resultdir'with open (FilePath,'R') as File_obj: whileTrue:#read the first line of data such as #[email protected]Lineinfo =File_obj.readline ()#set a flag to exit while Loop ifLen (Lineinfo) < 5: BreakMAILPSW= Lineinfo.split ('----') [-1] Mailstr= Lineinfo.split ('----') [0] Mailtype= Mailstr.split ('@') [ -1].split ('.') [0] Maildetail= Mailstr.split ('@') [0]#Save as new folder and file text operationsNewdirpath =Os.path.join (Resultdir,mailtype)#If this folder is not detected, create a if notos.path.exists (Newdirpath): Os.mkdir (Newdirpath)#after creating the type folder, store the new file information inNewfilepath = 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')defGetalldirit (path): Queue=Collections.deque ()#into the teamqueue.append (path)#Loop, when the queue is empty, stop looping whileLen (queue)! =0:#This is the equivalent of finding the absolute path to element a.Dirpath =Queue.popleft ()#Find all subdirectory information with directory, or file information with directoryDirlist =Os.listdir (Dirpath)#to traverse other information under this folder forFileNameinchdirlist:#Absolute PathDirabspath =Os.path.join (dirpath,filename)#judgment: If dir dir is queued for operation, if it is not ifOs.path.isdir (dirabspath):Print("Directory:"+filename) queue.append (dirabspath)Else: #find the normal file and process the information. Pass the path to this plain file.Work (Dirabspath)#Call to functionGetalldirit (R'e:\[aaa] (Thousand) Full stack learning Python\18-10-21\day7\newdir')
Results:
163 account password saved after mailbox processing
"14" file read and format processing