File Handling Process
- Open the file, get the file handle and assigned to a variable
- Operate on files handles
- Close the file
Open the file, there are three modes:
- Read-only mode (default)
- Write-only mode (unreadable, you create does not exist, then there is coverage)
- Append mode (read, does not exist is created, only the presence of the additional content)
"+" Indicates a file can be read and written simultaneously: 1. writable + R & lt file (readable, writable, can be added) 2. W + W R 3. a + additional "U" indicates the time of reading can be \ r \ r \ n autochanger \ n to \ n (r or r + using the simultaneous mode) because the system is Windows newline \ r \ n, newline Linux system is \ n, U is able to automatically add \ r \ n is converted into \ n- 1. rU 2. the U-R & lt + "B" represents the binary 1.rb 2.WB 3.ab
File operations Basic Usage
Basic usage: open ()
= Open the file_object (file_name, access_mode = 'R & lt', Buffering = -1) Open function has many parameters commonly used file_name, mode and encoding file_name: open the file name, if not the current path is to be noted that the specific path access_mode file open mode buffering may have a value of 0, 1,> 1 three, 0 for off buffer (only available in binary mode), Line 1 represents a buffer (for text mode only),> 1 indicates buffer size initialization; encoding represents the returned data is encoded using what is generally used utf8 or GBK;
# File Open Open () F = Open ( 'test.txt', 'R & lt +') # or with open () after the completion of this method of operation, will automatically turn off unneeded Close () with Open ( 'test.txt', 'R & lt') AS F: reached, f.read () # close the file F = Open ( 'test.txt', 'R & lt +', encoding = 'UTF-. 8') RET = reached, f.read () Print (RET) F. Close () # read the contents of the file (to specify each read word character) F = Open ( 'test.txt', 'R & lt +', encoding = 'UTF-. 8') RET reached, f.read = (. 8) Print ( RET) # reads data (read may specify the number of characters), stored as a display list F = Open ( 'test.txt', 'R & lt +', encoding = 'UTF-. 8') RET = f.readlines () Print ( RET) f.close () # read data line F = Open ( 'test.txt', 'R & lt +', encoding = 'UTF-. 8') RET = F.readline() print(ret) f.close() # write to file write () argument is a string Open = F ( 'test.txt', '+ A', encoding = 'UTF-. 8') f.write ( "ABC") RET = reached, f.read () Print (RET) f.close () # write files , the writelines () is a sequence of parameters, such as a list, it will help you to write the file iteration F = Open ( 'test.txt', '+ A', encoding = 'UTF-. 8') f.writelines ([ "AA", "BB", "CC"]) RET = reached, f.read () Print (RET) f.close () # determines whether the file is a unified tty device f = open ( 'test.txt', 'r +', encoding = ' . 8-UTF ') RET = f.isatty () Print (RET) #False f.close () # determines whether or not readable (unreadable error is "No File or Directory SUCH:") F = Open (' test.txt ',' R & lt + ', encoding =' UTF-. 8 ') RET = F.readable's () print (ret) #True f.close () # specified file position of the pointer f = open ( 'test.txt', 'r +', encoding = 'utf-8') ret = f.read (8) # 8 characters first read print (ret) f.seek (0) # and then move the pointer to the beginning of the file ret = f.read (8) # reread Print (RET) f.close () # Get pointer position f = open ( 'test.txt' , 'R & lt +', encoding = 'UTF-. 8') RET reached, f.read = (. 8) # 8 characters first read print ( "pointer position:% s "% f.tell ()) # Check current pointer position print (RET) f.seek (0) # reset to the start position designated print ( "pointer position:% s "% f.tell ()) # viewing the pointer position f.close () # truncate data file, retaining only before data (number of bytes specified) F = Open ( 'test.txt', 'R & lt +', encoding = 'UTF-. 8') f.truncate (. 8) # file keeping only the first 8 bytes of data, later in the file delete all the data RET = f.read () Print (RET) () f.close # file descriptor f.fileno () inside # refresh file buffer f.flush ()