In Python, everything is object.
First, the document operation process
(1) Open the file, get a file handle (object), assign to an object;
(2) operation of the file through a file handle;
(3) Close the file.
The file object F is created by using the open () function.
The open function commonly used three parameters: file path/name, mode, encoding.
1 f = open (' test file ','r', encoding=' Utf-8')2print(F.read ())3 f.close ()
Common values of mode parameters in open function
| Value |
Describe |
| ' R ' |
Read mode |
| ' W ' |
Write mode |
| A |
Append (Append) mode |
| ' B ' |
Binary mode (can be added to other modes for use) |
| + |
Read/write mode (can be added to other modes for use) |
When working with a sound clip or image, you should add ' B ' to the modulus parameter. The parameter ' RB ' can be used to read a binary file.
The ' + ' mode is used in addition to other modes:
| Value |
Describe |
| ' R+ ' |
Read/write mode. Read from the cursor 0 position, and the cursor will remain at the last position after reading, and then start writing from that position. |
| ' A + ' |
Append read mode. The cursor defaults to the last position. |
| ' w+ ' |
Read-write mode. Clear the contents of the file before writing. |
Iii. Methods of documentation
Close ()
Close () Closes an open file object that cannot be read or written until it is closed. Python automatically closes the previous file object when it is referenced to another.
Read ()
The read (size) method is used to read the specified number of bytes from the file, and if no size is specified, all is read.
1 f = open ('test','r')2 Print (F.read (5)) #读取5个字节 3 f.close ()
If you read Chinese characters, a character is considered to be one.
ReadLine ()
The ReadLine () method is used to read an entire line from a file, containing the "\ n" character.
If a non-negative parameter is specified, the number of bytes in the specified size is read, including the "\ n" character.
ReadLines ()
The ReadLines () method reads all rows (until the Terminator is EOF) and returns the list, which means that each row read is treated as an element of the list.
Returns an empty string if the Terminator EOF is encountered.
1 f=open (' Little Mountains ','a', encoding='UTF8 ')2print(F.readlines ())#[' The Cold family Meconematidae from last night. \ n ', ' surprised to get back to the dream, has shift. \ n ', ' Get up and go around the steps alone. \ n ', ' whispered the man, the curtain is dim outside the moon. \ n ', ' the oldest for fame, old mountain pine bamboo old, resistance return journey. \ n ', ' to Shang the mind. \ n ', ' The bosom friend is few, the chord is broken who listens. ']
Write ()
The Write () method is used to write the specified string to the file.
The contents of the string are stored in the buffer before the file is closed or before the buffer is flushed, and the content written is not visible in the file.
Writelines ()
This method is used to write a sequence of strings to a file. You need to specify a newline character \ n when swapping lines.
1 fo = open ( " test.txt , " w ) print ( " file name: " 3 seq = [ oliver\n , " alex ] 4 fo.writelines (seq)
Tell ()
Returns the current position of the file, which is the current position of the file pointer.
Seek ()
Used to move a file to read a pointer to a specified location.
Offset--The starting shift, which represents the number of bytes that need to be shifted
whence: optional, default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.
Truncate ()
Used to truncate a file, if an optional parameter of size is specified, the truncated file is a size character. If size is not specified, the
Flush ()
Used to flush buffers, the data in the buffer is immediately written to the file, and the buffer is emptied, without the passive waiting for the output buffer to write.
In general, the buffer will be refreshed automatically when the file is closed, but if the data security requirements are high, avoid losing data in the event of a sudden power outage, you need to refresh it before closing, you can use the flush () method.
Example of a progress bar:
1 Import Sys,time 2 for in range:3 sys.stdout.write ("*") 4 Sys.stdout.flush () 5 Time.sleep (0.1)
Iv. with statements
To avoid forgetting to close a file after you open it, you can use the WITH statement to automatically manage the context.
1 with open (' County _new','w', encoding=' Utf-8') as F_write:2 f_write.write (str (current_layer))
Python Learning notes-file manipulation