Python Learning Day07

Source: Internet
Author: User

One: File operation

#Codeing:utf-8#__author__:duke#date:2018/3/4/004#Learn file operations today##读文件f = open ('Information','R', encoding='Utf-8')#opens a File object, in the form of a readdata = F.read (5)#With the Read method, the entire contents of the argument are the number of characters when no argument is given .Print(data) f.close ()#Close File###写文件f = open ('Information2','W', encoding='Utf-8')#open a File object, write the wayF.write ('Hello World!')#and use the Write methodF.write ('Duke') F.close ()#Close File###追加文件内容f = open ('Information2','a', encoding='Utf-8')#opens a File object, appendedF.write ('Hello ****!')#and use the Write methodF.write ('Duke') F.close ()#Close File#r+ mode is appended at the endf = open ('Information','r+', encoding='Utf-8')#opens a File object, in the form of a readdata = F.read (5)#With the Read method, the entire contents of the argument are the number of characters when no argument is given .Print(data) F.write ('\njoin') F.close ()#Close File#clear first, can be read after writingf = open ('Information','w+', encoding='Utf-8')#opens a File object, in the form of a readF.write ('Join')Print(F.tell ())#after finishing the cursor at the endF.seek (0)#adjust the cursor before you can readPrint(F.readline ()) F.close ()#Close File#Copying FilesF_read = open ('Information2','R', encoding='UTF-8') F_write= Open ('Information3','a', encoding='UTF-8') Num=0 forLineinchF_read:num= Num+1ifnum = = 6: Line='Hello world\n'F_write.write (line) F_write.close () f_read.close ()

Two: Methods of File objects

#Codeing:utf-8#__author__:duke#date:2018/3/4/004#Read the filef = open ('Information','R', encoding='Utf-8')#opens a File object, in the form of a readnum =0; forIinchF:#the iterator method reads the data, that is, a row to fetch a rowNum +=1ifnum = = 6: I="'. Join ([I.strip (),'III'])    Print(I.strip ()) F.close ()#Close File##tell方法f = open ('Information','R', encoding='Utf-8')#opens a File object, in the form of a readPrint(F.tell ())#Tell method read cursor positionPrint(F.read (1))Print(F.tell ()) f.seek (0)#Adjust cursor positionPrint(F.tell ())Print(F.read (1))Print(F.tell ()) F.close ()#Close FileImportTIMEF= Open ('Information2','W', encoding='Utf-8')#opens a File object, in the form of a readF.write ('Duke')#With the Read method, the entire contents of the argument are the number of characters when no argument is given .F.flush () time.sleep (10) Time.sleep (10) F.close ()#Close File#Flush MethodImportTime,sys forIinchRange (30): Sys.stdout.write ('*') Sys.stdout.flush () Time.sleep (0.1)#Truncate truncation Methodf = open ('Information2','a', encoding='Utf-8')#opens a File object, appendedF.truncate (2) F.close ()#Close File

Three: Working time (three-level menu additions and deletions to check and change)

#Codeing:utf-8#__author__:duke#date:2018/3/4/004file= Open ('Information','r+', encoding='UTF-8') Data=file.read () data=eval (data) file.close ();#For key in data:#print (key)Current_layer = data;#record your current levelParent_layers =[];#List Store Entry pathFlag =0; whileTrue: forKeyinchCurrent_layer:Print(key);#Looping the Print menuChoice = input ("[B: Return, D: Delete, a: Add, R: Modify]>>>:"). Strip ()#input, and remove extra characters in front and back of the space form    ifLen (choice) = = 0:Continue; ifChoiceinchcurrent_layer:parent_layers.append (Current_layer)#Before entering the next layer, put his first layer into the list of paths to goCurrent_layer = Current_layer[choice];#assigns the next layer to the current layer    elifChoice = ='b':        ifParent_layers:current_layer= Parent_layers.pop ();#takes out its parent layer and assigns it to the current layer        elifFlag ==1 :             Break; Else: Flag+=1Print("is already the first level, and pressing once will exit the program")    elifChoice = ='D': Value= Input ("Enter the value you want to delete:>>") Current_layer.pop (value)elifChoice = ='R': value1= Input ("Enter the value you want to modify:>>") Current_layer.pop (value1) value2= Input ("Enter the value you want to modify:>>") value2={value2: {}} current_layer.update (value2)elifChoice = ='a': Value= Input ("Enter the value you want to add:>>") Value={value:{}} current_layer.update (value)Else:#Judging Illegal input        Print("no this option ...");Print(data) file2= Open ('Information','W', encoding='UTF-8') Data=str (data) file2.write (data) file2.close ()

Python Learning Day07

Related Article

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.