Use the Open function to create a file object, and then you can use the functions commonly used by file objects (files).
Example: F = open ('/path/to/targetfile/test.txt ', mode= ' R ', encoding= ' utf-8 ')
Open function Parameter Description:
First location: Name of File
Second position: mode value, which determines the pattern of open files: Read Only (R), write only (W), append (a), etc. default is read-only
Third Position: encoding value, read the file is what encoding format, here is what encoding format, and read the file consistent
Common functions for File objects
Serial Number |
function |
Description |
1 |
File.read ([size]) |
Reads the specified number of characters from the file, and reads everything in the file if no size is specified |
2 |
File.write (str) |
Writes a string to a file, returning the length of the string being written |
3 |
File.close () |
Close File Object |
4 |
File.flush () |
Refreshes the internal buffer of the file, directly writes the data of the internal buffers to the file immediately |
5 |
File.readline () |
Reads a line of files, including newline characters ' \ n ' |
6 |
File.readlines () |
Reads all the lines of a file one time, returns a list of individual elements in the list that are single-line content of the file |
7 |
File.seek (Offset[,where]) |
Move file read pointer position <br/> parameter: <br/>Offset-offset (in bytes)<br/>where-optional ( 3 values: 0 offset from beginning of file 1 offset from current position 2 from end of file |
8 |
File.truncate ([size]) |
Truncate <br/> parameter: When size is specified, from the beginning of the file, size bytes <br/>size unspecified, truncated from the current position to the end of the file, and the contents of the current position value are deleted . /c9> |
Full list of open files in different modes
Mode |
Description |
R |
Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files and so on. |
r+ |
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
rb+ |
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. Generally used for non-text files and so on. |
W |
Open a file for writing only. If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. |
Wb |
Open a file in binary format only for writing. If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. Generally used for non-text files and so on. |
w+ |
Open a file for read-write. If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. |
wb+ |
Opens a file in binary format for read-write. If the file already exists, open the file and edit it from the beginning, that is, the original content is deleted. If the file does not exist, create a new file. Generally used for non-text files and so on. |
A |
Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
Ab |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
A + |
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
ab+ |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. |
Specify binary mode when storing pictures, video, and often use ' WB ' mode when doing crawl storage images.
code example:
# There are testfile.txt, content for I love you, China's beloved mother, and I'm proud of you for your tears.
1. Reading files
# reads everything and then outputs
F = open ( testfile.txt ", Mode=" R ", Encoding=" utf-8 " ) content = F.read () print
# reads the contents of all rows at once, traverses the output f = open ( " testfile.txt " , Mode= " r , Encoding= " utf-8 " ) file_lines = F.readlines () for line in file_lines: print (line, End=" ) f.close ()
# reads the specified number of characters f = open ('testfile.txt', mode='r', encoding= ' Utf-8 ' = f.read (3) # Note Here is the number of characters, not the number of bytes, if the number of bytes, utf-8 encoding a Chinese account for 3 bytes, then the output should be ' I 'print (s) f.close () # output Results I love you.
2. Writing files
#empty the original content to writef = open ('testfile.txt', mode='W', encoding='Utf-8') F.write ('I'm really proud.') F.flush () f.close ()#Append Writef = open ('testfile.txt', mode='a', encoding='Utf-8') F.write ('I'm really, really proud.') F.close ()
3. Pointers to operating files
#manipulate the file to read the pointer positionf = open ('testfile.txt','R') F.seek (3)#read pointer 3 bytes right SHIFTPrint(F.read (2))#Read 2 charactersF.seek (6)Print(F.read (1)) F.seek (3, 0)#The read pointer moves from the beginning of the file to the right 3 bytesPrint(F.read (1))Print(F.tell ())#returns the current position of the file pointer#F.seek (3,1)Print(F.readline ())#print (F.read ())f.close ()#Output ResultsLove You, you love6you China
Python Learning-File manipulation