First, open the file mode 1: read, write, append
1, r Read: read mode, read-only, can not write, the file open error does not exist; may be omitted 'r', since the read mode is by default not write
f = open('filename',encoding='utf-8') result = f.read() print(result) f.close()
2, w Write: Write mode, can write, can not read, it will overwrite the original contents of the file does not exist will be created
f = open('test.txt','w',encoding='utf-8') f.write('abc')
3, a Append: append mode, can write, can not read, add new content on the contents of the original file; file creates the file does not exist
f = open('test.txt','a',encoding='utf-8') f.write('哈哈') f.close()
Second, the file open mode 2: r +, w +, a +
1, r +: For as long as r, open the file does not exist will be given, the default file pointer + r at the top;
2, w +: w and about as long, will clear the original document;
3, a +: the default file pointer is at the end, if you want to read the content, first put the file pointer is moved to the front;
Since the pattern is behind a + additional content, the original content is not clear, it is possible to use a + mode using
Third, several ways to read the file:
read (): one-time read the entire contents of the file. Recommended read (size), and the longer running time of greater size
readline (): read every single line. Not enough memory use, generally do not use
readlines (): one-time read the entire contents of the file, and press OK to return to the list, to help us traverse
We are generally small files using read (), you're sure about the size of a given size, large file to use readlines ()
Open = F ( ' new.txt ' , encoding = ' UTF-. 8 ' ) # Print (reached, f.read ()) of all the contents of the file read # # Print (f.readline ()) reads the contents of the file line # # Print (f.readline ()) # read the file line content Print (f.readlines ()) # All content will read all the contents into the list on display in f.seek (0) # move files pointer Print (reached, f.read ()) f.close ()
Fourth, the contents of the list of the incoming file method:
1. Method 1:
# The contents written to the file list new.txt l in l = [ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ] F = Open ( ' new.txt ' , ' W + ' , encoding = ' UTF-. 8 ' ) for I in L: f.write (I) f.seek (0) Print (reached, f.read ()) f.close ()
2. Method 2:
f.writelines (L) # pass a list of words, he will automatically help you cycle, every element of the list which wrote the document inside f.seek (0) Print (f.read ()) f.close ()
Fifth, modify the file
# 1, straightforward brutal manner # (1) to read the contents # (2) and then replaced # (3) Clear the contents of the original file # (4) write the new contents into the F = Open ( ' old.txt ' , ' A + ' , encoding = ' UTF-. 8 ' ) f.seek (0) Result = reached, f.read () Content = result.replace ( ' Xiaolan ' , ' Dalan ' ) f.seek (0) f.truncate ( ) # Clear file contents f.write (content) F2 = Open ( ' smh.txt' , ' W ' ) f2.write (Content) # 2, the progressive process # (1) open a file, the file open empty file b # (2) after a line is read from a file, edit, modify files written in b where # (3) delete a file, the name of the file into a file b F = Open ( ' case.txt ' , encoding = ' UTF-. 8 ' ) F2 = Open ( " case2.txt " , ' W ' , = encoding ' UTF-. 8 ' ) for Line in F: Result = line.upper () f2.write (Result) f.close () f2.close () import os os.remove('case.txt') os.rename('case2.txt','case.txt')
Sixth, do not close the document:
# With method does not close the file, automatically closes with Open ( ' new.txt ' ) AS F: reached, f.read () with Open ( ' new.txt ' ) AS F, Open ( ' new_w + ' , ' W ' ) F2 AS: F = reached, f.read () f2.write ( ' XX ' )