read:
1 f=open( " Deceive yourself " , ' r ' ,encoding= ' utf8 ' ) #Open the specified file and get the file handle "f=open("/path/file name", 'operation mode', encoding='specified Encoding format ')" r: read-only mode 2 data=f.read() #read all content 3 # data=f.read(10 ) #read n(10) bytes (the read method considers a Chinese It's also a byte, that's it) 4 print (data) 5 print(f.readline().strip()) # The readline method can output a line, the print output will output a newline by default, and the strip() method is to clear The newline of print , where the cursor is, it will start to output 6 print(f.readlines()) #Output all the contents of the file in the form of a list,
7 f.close() #Close the file (python will automatically perform the closing operation by default, but you still have to specify it yourself. Because the common people have a famous saying, believe in yourself and you will live forever)
Read everything with the readlines() method and add a comment on the sixth line:
1 f=open( " deceive yourself " , ' r ' ,encoding= ' utf8 ' ) 2 data= f.readlines() 3 f.close() 4 for i,n in enumerate(data): 5 if i == 6 : 6 n= "" .join([n.strip(), " #I can't sing this sentence " ]) 7 print (n.strip())
The readlines() method is to put all the content in the file into In memory, and then read out together, it will have a great impact on performance for large files, and it is not recommended to use
For reading large files it is recommended to use the following methods:
1 f=open( " Cheat yourself " , ' r ' ,encoding= ' utf8 ' ) 2 for i in f: 3 print (i.strip()
alone outputs f is a memory object: <_io.TextIOWrapper name='Cheat My own ' mode='r' encoding='utf8'>
The for statement in python performs iterator processing on the read memory object, (iterator: use one line, load one line, delete it and then continue to load the next line)
It must be an object that can call a method
---- Alex's apprentice said let me remember this sentence
Write:
1 f=open( " Deceive yourself " , ' w ' ,encoding= " utf8 " ) # w: write-only mode, if the file does not exist, it will be created, if there is content in the file, it will be emptied 2 f.write( " If you become rich one day " ) #Write to memory 3 f.write( " \nMy first choice is not to travel around the world " ) # \n is a newline, python will not follow the default You added, because a memory block defaults to 4k... and when writing content, it will be written according to the position of the cursor... Forget it, you understand it. 4 f.close() #Close the file and put The contents of memory are loaded to disk
addition:
1 f=open( " Deceive yourself " , ' a ' ,encoding= ' utf8 ' ) # a: for append mode, if the file does not exist, it will be created, move the cursor to the end of the file 2 f.write( " Lying in the world In the largest and softest sofa " ) #Write to memory 3 f.write( " \rAfter eating, wake up and eat for a year before eating " ) # \rAlso TMD is a line break, very strong 4 f.close () #Close the file and load the contents of memory to disk
Read, write, append upgrades:
1 f=open( " Deceive yourself " , ' r+ ' , encoding= ' utf8 ' ) # r+: read and write mode 2 print (f.readline()) #Read the line where the cursor is 3 f.write( " - -------------------- " ) #The operation of writing will be appended to the end of the file 4 5 f=open( " Deceive yourself " , ' w+ ' ,encoding= ' utf8 ' ) # w+: write and read mode, create a file if it does not exist, and clear the file content if it exists 6f.write( " --------------------- " ) #The write operation will be appended to the end of the file 7 print (f.seek(0)) 8 print ( f.readline()) #After writing , the cursor is at the end of the file, so the content cannot be read. You need to move the cursor to the position of 0 to read 9 10 f=open( " Cheat yourself " , ' a+ ' , encoding= ' utf8 ' ) # a+: Additional reading, the cursor will be adjusted directly at the end 11 print (f.readline()) #The cursor is at the default, no content is directly read 12 f.write( " -------- ----- ") #The content written will be directly appended to the end of the file
Other operations:
1 print (f.tell()) # The tell() method gets the current cursor position 2 f.seek(5) #Specifies the cursor position
Can be used as a breakpoint resume
3 f.flush() # flush the contents of the cache directly to the memory