This article is participating in Python Theme Month. See the link for details

Retrospective review

In the file management analysis, we carried on the whole framework of file management basic understanding of file classification and basic Python operation steps

In the file close flow know file operation after completion, to close, release computer resources; File read and write chapter, the basic file read and write operations and usage to learn

In this installment, we’ll summarize what we learned in the previous chapters and introduce common ways to use files in a work situation.

1. File object properties

Properties of the file object:

attribute instructions
name Returns the name of the file
mode Returns the open mode of the file
closed Returns True if the file is closed

2. Open file object mode

model instructions
r Read mode
w Write mode
a Append mode
b Binary mode (can be combined with other modes)
+ Read/write mode (can be combined with other modes)

2. File object method

The method name instructions
read([size]) Returns size bytes or characters read from a file. If [size] is omitted, the end of the file is read, that is, all the contents of the file are read at once
readline() Reads a line from a text file
readlines() Each line in the text file is returned as a separate string object and these objects are placed in a list
write(str) Writes the string STR content to the file
writelines(s) Writes the string list S to a text file without adding a newline character
seek(offset,[,whence]) Move the file pointer to a new location,
tell() Returns the current position of the file pointer
truncate([size]) No matter where the pointer is, only the size of the first byte of the pointer is left, and the rest is deleted. If size is not passed, then when the current position of the pointer is at the end of the file, everything is deleted. Okay
flush() Writes the contents of the buffer to a file, but does not close the file
close() Writes the buffer contents to a file, closes the file, and frees the file object related resources

3. File Pointers

A file pointer is used to indicate the starting position of a file read or write.

The file object provides the tell() function and seek() function to move the file pointer.

  • The tell() function is used to determine where the file pointer is currently located

  • The seek() function is used to move the file pointer to the specified location of the file

3.1 the tell

The use of the tell() function is simple, and the basic syntax is as follows:

File. Tell ()Copy the code

The tell function returns that the position of the file pointer is more than one byte away from the beginning.

With open (" a.t xt ", "r") as f: print (" pointer initial position: ", f.t ell ()) print (" read content: ", f.r ead (3) print (" pointer position: after reading ", f.t ell ())Copy the code

3.2 the seek
seek(offset,[,whence])
Copy the code

Offset represents the offset of how many bytes relative to whence

Offset: off indicates positive movement towards the end direction and negative king start direction

whence Different values represent different meanings:

0: evaluates from the file header (default);

1: calculates from the current position.

2: calculates from the end of the file.

Waring: When offset is not 0, Python requires that the file be opened in binary format; otherwise, an IO.UnsupportedOperation error will be thrown.

with open("a.txt","rb") as f: F.tell () : print(" ",f.tell()) : print(" ",f.tell()) ",f.tell() # print(",f.tell() ",f.tell() # print(",f.tell() ",f.tell() # print(",f.tell() ",f.tell()) # print(",f.tell() ",f.tell() # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) # print(",f.tell()) #Copy the code

IO.UnsupportedOperation error IO.UnsupportedOperation error IO

IO.UnsupportedOperation error is reported when we change the file opening mode to R mode and run it again

conclusion

In this issue, we mainly summarize the previous knowledge points related to file management, and explain some strange methods such as seek() and tell() above. We use these methods in our work. As the saying goes, “Good work must first sharpen its tools” to lay a solid foundation.

This is the content of this episode, welcome you to comment and like, see you next time ~