Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set read and write position in a file in Python?
File positioning allows you to navigate to specific locations within a file for reading or writing operations. Python provides the seek() and tell() methods to control the file pointer position, enabling precise file manipulation.
The seek() Method
The seek() method moves the file pointer to a specified position. Its syntax is file.seek(offset, whence), where:
offset: Number of bytes to move
whence: Reference point (0=start, 1=current, 2=end)
Setting Read Position
You can set the read position to start reading from any location in the file ?
# Create a sample file first
with open('sample.txt', 'w') as f:
f.write("This is a test file for positioning")
# Open file and set read position
with open('sample.txt', 'r') as file:
# Move to 10th character
file.seek(10)
# Read from new position
content = file.read()
print("Content from position 10:", content)
Content from position 10: test file for positioning
Setting Write Position
For write operations, you need to open the file in appropriate mode. Note that 'w' mode truncates the file ?
# Create initial content
with open('sample.txt', 'w') as f:
f.write("Hello World")
# Open in r+ mode to read and write without truncating
with open('sample.txt', 'r+') as file:
# Move to position 6
file.seek(6)
# Write at new position
file.write("Python")
# Read entire file to see result
file.seek(0) # Go back to start
print("File content:", file.read())
File content: Hello Python
Using tell() to Get Current Position
The tell() method returns the current file pointer position ?
with open('sample.txt', 'r') as file:
print("Initial position:", file.tell())
# Read some characters
file.read(5)
print("Position after reading 5 chars:", file.tell())
# Move to specific position
file.seek(0)
print("Position after seek(0):", file.tell())
Initial position: 0 Position after reading 5 chars: 5 Position after seek(0): 0
Working with Different Whence Values
The whence parameter determines the reference point for positioning ?
with open('sample.txt', 'w') as f:
f.write("0123456789")
with open('sample.txt', 'r') as file:
# From beginning (whence=0)
file.seek(3, 0)
print("From start, position 3:", file.read(1))
# From current position (whence=1)
file.seek(2, 1)
print("From current + 2:", file.read(1))
# From end (whence=2)
file.seek(-3, 2)
print("From end - 3:", file.read(1))
From start, position 3: 3 From current + 2: 6 From end - 3: 7
Practical Example: Modifying Specific Parts
Here's a practical example of updating specific content in a file ?
# Create a data file
with open('data.txt', 'w') as f:
f.write("Name: John\nAge: 25\nCity: New York")
# Update the age field
with open('data.txt', 'r+') as file:
content = file.read()
print("Original content:")
print(content)
# Find and update age
age_pos = content.find("Age: ") + 5
file.seek(age_pos)
file.write("30")
# Display updated content
file.seek(0)
print("\nUpdated content:")
print(file.read())
Original content: Name: John Age: 25 City: New York Updated content: Name: John Age: 30 City: New York
File Modes for Positioning
| Mode | Read | Write | Truncates File |
|---|---|---|---|
| 'r' | Yes | No | No |
| 'r+' | Yes | Yes | No |
| 'w+' | Yes | Yes | Yes |
| 'a+' | Yes | Yes (at end) | No |
Conclusion
File positioning with seek() and tell() provides precise control over file operations. Use r+ mode for reading and writing without truncating existing content. Always use context managers (with statements) for proper file handling.
