SoFunction
Updated on 2024-11-21

How python gets the current location of a file and locates a certain location

I. Background

Python is an easy-to-learn, powerful programming language. It provides efficient high-level data structures and also allows for simple and effective object-oriented programming.

Python's elegant syntax and dynamic typing, as well as its interpreted nature, make it an ideal language for writing scripts and rapidly developing applications on most platforms.

Here we will introduce python's file pointer positioning related knowledge.

II. Getting the current read/write position

During the process of reading or writing a file, if you want to know the position of the current file location pointer, you can get it by calling the tell() method.

The tell() method returns the current position of the file, i.e. the current position of the file location pointer.

The syntax is formatted as follows:

  • File object name.tell()

Example:

Use the tell() method to get the current read and write positions of the file.

with open('','r') as file:	# Open a file named "" in read-only mode
    line = (8)		# Read the first 8 bytes
    print(line)			# Output the first 8 bytes
    p = ()			# Get the current position of the pointer
    print('Current location:',p)		# Output current position
    line = (4)		# Continue reading 4 bytes
    print(line)			# Output the read data
    p = ()			# Get the current position of the pointer
    print('Current location:',p) 		#Output current position

The results are as follows.

III. Positioning to a location

If in the process of reading or writing a file, you need to start the read/write operation from a specified location, you can use the seek() method to realize it.

The seek() method is used to move the file location pointer to the specified location.

The syntax is formatted as follows:

  • File object name.seek(offset[,whence])

Example:

Create a file named "", type "This is a test!" and save it in the file, read the word "test" and output it to the terminal.

filename = input('Please enter the name of the newly created file:')		# Input filename
with open(filename,'w+') as file:			# Create a new file and open it as read/write
    ('This is a test!')			# Inputting strings to a file
    (10)					# Pointer moves to 10th character from beginning
    con = (4)				# Read 4 characters to con
    print(con)					#exports

The results are as follows.

幻灯片22

When opening a file in text file format, the whence parameter in the seek() method can only take a value of 0, i.e., only offsets from the start of the file are allowed to be calculated.

To calculate the offset from the current position or from the end of the file, you need to open the file in "b" mode (binary format).

Example:

Reads the penultimate character in the "" file.

with open('','rb') as file:		# Create a new file and open it as read/write
    (-2,2)				# Position the file location pointer to the penultimate character
    con = (1)			# Read 1 character to con
    print(con)				#exports

The results are as follows.

IV. Summary

The above is the knowledge about Python's file pointer positioning, only for personal experience, I hope to be able to give you a reference, and I hope you support me more.