1. Open () File.close ()
# closed file can no longer read and write operations, otherwise it will trigger valueerrorfo = open ('a.txt'r' print(' open a.txt file ') fo.close ()
2. Flush ()
# Flush () flushes the buffer and writes the buffer's data immediately to the file # while emptying the buffer, it is not necessary to wait for the output buffer to write # Normally, the buffer is refreshed automatically when the file is closed, but sometimes it needs to be refreshed before it is closed, and the flush () method is required fo =open ('a.txt' WB ' Print(' open a.txt file ') Fo.flush () fo.close ()
3. Open the file mode
①. read-only mode R (default)
②. Write-only mode W (not readable, not present, created, overwrite if present)
③. Append mode A (readable, not present, created, only append content exists)
④. ' + ' indicates that a file can be read and written:
R+ can read and write files (readable, writable, can be appended)
w+ Write Read
A + append
⑤. ' u ' means the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n (simultaneous use with R or r+ mode) is available when reading
Because the line break for the Windows system is \ n for the \r\n,linux system, plus u can automatically convert \ r \ n
Ru
R+u
⑥. ' B ' means processing binary files
Rb
Wb
Ab
4.
# # Read the contents of the file (you can specify the number of characters per read) # With open (' a.txt ', ' r+ ', encoding= ' utf-8 ') as F: # ret = f.read (8)# print (ret)
5.
# read data (can specify number of characters to read), Save as List display # f = open (' B.txt ', ' r+ ', encoding= ' utf-8 ') # ret = f.readlines () # print (ret) # f.close ()
6.
# # Read a row of data # f = open (' B.txt ', ' r+ ', encoding= ' utf-8 ') # ret = f.readline () # print (ret) # f.close ()
7.
# determine if the file is readable (unreadable and error "No such file or directory:")f = open ('b.txt'r+ ', encoding='utf-8'= f.readable ()Print (ret) f.close ()
8.
#Get pointer positionf = open ('B.txt','r+', encoding='Utf-8')#Read 8 characters Firstret = F.read (8)#view current pointer positionPrint('pointer position:%s'%F.tell ())Print(ret)#Reset pointer to start bitf.seek (0)#View the current position of the pointerPrint('pointer position:%s'%F.tell ()) F.close ()
9.
#specify pointer position in filef = open ('B.txt','r+', encoding='Utf-8')#Read 8 characters Firstret = F.read (8)Print(ret)#Reset pointer to start bitf.seek (0)#8 characters are re-readret = F.read (8)Print(ret) f.close ()
10.
# intercepts the file data, preserving only the specified previous data (specified number of bytes)f = open ('b.txt'r+', encoding='utf-8')# file retains only the first 8 bytes of data, the data behind the file is all deleted F.truncate (8= f.read ()print(ret) f.close ()
Fileno ()
# Fileno () returns the file description of an integral type (file Desccriotor FD integer) # I/O operation for the underlying operating system FO = open ('b.txt'wb') Print ( ' file name:'= Fo.fileno ()print(' File Description:', FID) fo.close ()
Isatty ()
# Isatty () detects if the file is connected to an end device # If True, returns falsefo = open ('b.txt'wb') Print(' file name:'= fo.isatty ()Print (' return value:', ret) fo.close ()
Next ()
#the file object in Python3 does not support the next () method#Python3 's built-in function next () calls the __next () __ method from the iterator to return to the next item#In a loop, the next () method is called in a loop that returns the next line of the file#Trigger stopiteration If End of Reach (EOF) is reachedFO = open ('B.txt','R') forIndexinchRange (5): Line=Next (FO)Print('Line%d-%s'% (index, line))
Python file Operations