"14" file read and format processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.