First, the document processing process
f = open ('file_management', ' R ', encoding='utf-8') # The default is read-only mode # file handle data =f.read ()print(data) f.close ()
Second, the way to open the file
W Mode: File does not exist, new file, file exists, directly overwrite; no matter what, create a new empty document overwrite the original document directly
f = open ('New_file','W', encoding='Utf-8')#file handlePrint(F.writable ())#the content written must be a stringF.write ('11111111\n') F.write ('22222222\n') F.writelines (['3333\n','44444\n']) f.close ()
A mode: Append mode
f = open ('new_file','a', encoding='utf-8 ')
r+ mode: Readable and writable (start at cursor)
f = open ('new_file','r+', encoding=' Utf-8 ')
Other modes:
# r+ Read Write # w+ write # x+ Write Read # write read # "B" means to operate in bytes, read to bytes, and write to Byte # rb=r+b# wb=w+b # xb=x+b # ab=a+b
f = open ('New_file','RB') Data=F.read ()Print(data)Print(Data.decode ('Utf-8') ) F.close ()#windows inside the line is \ r \ n; Linux is \ nF= Open ('New_file','WB') f.write (bytes ('111\n', encoding='Utf-8') ) F.close ()#Write and overwrite
With mode open without shutting down
With open ('file','w') as F, Open (' file_new ','w', ending='utf-8'): # file Operations
Third, the method of operating the file
Files do not care about the modification, essentially overwrite, the process of modification is to first open the old file, read the content and then re-write and overwrite
The nature of the modification: Open a file, a line to read into the memory, read a line into the new file, the process can be modified, and then loaded into the new file, overwriting the original file
f = open ('file_management','R', encoding='Utf-8')#file handlePrint(F.readable ())Print('-'*80)Print(F.readline (), end ="')Print(F.readline (), end ="')Print(F.readline (), end ="')Print('-'*80)Print(F.read ()) F.close ()
Notice that the cursor moves, and once read () reads all the contents, the cursor moves to the last
ReadLine () read one line at a time, notice that there is a carriage return at the end of each line \ n
ReadLines () all read into a list, one line is an element
Iv. Other methods of file operation
1. Is the f.closed file closed?
2. Flush () swipe into the hard drive
3. Tell () Current position
4. Seek (point) moves the cursor position, in addition to the read is the character, the rest of processing is processing bytes
new_file1234567890
f = open ('New_file','RB')#To use the second and third modes, you must use the B-mode and do not specify encodingPrint(F.tell ()) F.seek (10)Print(F.tell ()) F.seek (3)Print(F.tell ()) F.seek (3,0)#first mode, default modePrint(F.tell ()) F.seek (3,1)#second mode, relative positionPrint(F.tell ()) F.seek (-3,2)#Third mode: Seek is inverted from the end of the filePrint(F.tell ())Print(F.read ())
result:0103367b'890'
The third way to use: Log file processing
# How to loop the file For i in f: print (i) # consuming memory for i in F.readlines (): print (i)
new_file 2018/8/1 Alex did something. 2018/ 8/1 Alex did something. 2018/8/1 Alex did something. 2018/ 8/1 Alex did something. 2018/8/1 Alex did something. 2018/ 8/1 Alex did something. 2018/8/1 Alex did something. 2018/ 8/1 Alex did something 2018/8/2 Joey did something
Offs = Ten # offset while True: f.seek (-offs,2) = F.readlines () if len (data) >1: print(' the last line of the file is %s' % (Data[-1].decode (' utf-8')) break Else: offs*=2Result: The last line of the file is 2018/8/2 Joey did something
5. Read (num) reads in NUM characters
6, truncate (num) intercept [0, num) (File open mode cannot choose w+, so as not to open the original file empty
Python file processing