Introduction
Welcome to my 51st blog post on the Python journey. On day 51, I learned about some file-handling functions that can be used to move to a specific position in a file. I learned about seek(), tell() and truncate() functions in Python. Now Let's dive into more details and understand File handling in Python.
So let's get started......
seek() and tell() functions
They are used to work with file objects and their positions in a file.
They are part of the built-in io module, which provides a consistent interface to various file-like objects, such as files, pipes, and in-memory buffers for reading and writing.
seek()
It allows moving the current position within a file to a specific point.
This position is specified in bytes, and we can move either forward or backward from the current position.
Example -
with open('file.txt', 'r') as f:
# Move to the 10th byte in the file
f.seek(10)
# Read the next 5 bytes
data = f.read(5)
Here we create a file named 'file.txt' and we use seek() to move to a specific position in the file and read the contents from that position i.e from the 5th byte as in the given example.
tell()
It returns the current position within the file, in bytes.
This can be useful for keeping track of your location within the file or for seeking a specific position relative to your current position.
Example -
with open('file.txt', 'r') as f:
# Read the first 10 bytes
data = f.read(10)
# Save the current position
current_position = f.tell()
# Seek to the saved position
f.seek(current_position)
truncate()
When we open a file using the open function in Python, we specify the mode in which we want to open the file such as read 'r', write 'w', or append 'a'.
But if we want to truncate the file to a specific size, we can use the truncate function. This restricts the file size to the truncated size.
Example -
with open('sample.txt', 'w') as f:
f.write('Hello World!')
f.truncate(5)
with open('sample.txt', 'r') as f:
print(f.read())
In the above example, the file is restricted to writing to 5 bytes i.e only 'Hello' 5 bytes will be printed in the file and the rest content will not be printed.
Resources Used
You can watch the video of Day#51 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 51, I learned about reading and writing into a file using seek(), tell() and truncate() functions in Python. This allows us to customize the content in the file and make us read and write content to specific file sizes and fetch us our current position pointer in the file.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #51
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.