seek() function?
Regarding seek()
there's not too much to worry about.
First of all, it is useful when operating over an open file.
It's important to note that its syntax is as follows:
fp.seek(offset, from_what)
where fp
is the file pointer you're working with; offset
means how many positions you will move; from_what
defines your point of reference:
- 0: means your reference point is the beginning of the file
- 1: means your reference point is the current file position
- 2: means your reference point is the end of the file
if omitted, from_what
defaults to 0.
Never forget that when managing files, there'll always be a position inside that file where you are currently working on. When just open, that position is the beginning of the file, but as you work with it, you may advance.seek
will be useful to you when you need to walk
along that open file, just as a path you are traveling into.
Seek function Python
When you open
file in text mode, the file-like object it returns is io.TextIOWrapper
(derived from io._TextIOBase
). Their seek()
with regard to whence
works as follows:
The default value for whence is SEEK_SET.
SEEK_SET
or0
: seek from the start of the stream (the default); offset must either be a number returned byTextIOBase.tell()
, or zero. Any other offset value produces undefined behaviour.
SEEK_CUR
or1
: “seek” to the current position; offset must be zero, which is a no-operation (all other values are unsupported).
SEEK_END
or2
: seek to the end of the stream; offset must be zero (all other values are unsupported).
I.e. you can use whence
values of 1
and 2
only with offset
of 0
.
seek function doesn't work to update a file in a specific position - python
Quoting from tutorialspoint.com,
Note that if the file is opened for appending using either 'a' or 'a+', any seek() operations will be undone at the next write.
"a" mode write
operations append to the end of the file. What seek
does is it sets the write/read pointer to a specific location in the file.
Therefore, when a write
is called, it will write to the end of file, regardless of the read/write pointer.
However, because you've opened the file in a+b
, you would be able to seek to a specific location and read
it.
How can I use seek() in a binary file?
seek
changes the file postion but doesn't read anything. It wouldn't know in general how much to read. After the seek you can read 1 byte. As a side note, don't open with more rights than you need - no need to create an unnecessary failure point in your code.
def openFile():
itaFile = filedialog.askopenfilename(
filetypes=[("ITA Files", ".ITA"), ("All Files", "*")])
with open(itaFile, "rb") as itaOpened:
a = itaOpened.seek(6)
a = itaOpened.read(1)
print(a)
How does Python's seek function work?
It is OS- and libc-specific. the file.seek()
operation is delegated to the fseek(3)
C call for actual OS-level files.
Python seek() not shifting pointer to right place
you need to flush()
the buffer to disk, because write
is happening in the buffer in memory not the actual file on the disk.
In the second scenario, before readline()
you are calling f.tell()
which is actually flushing the buffer to disk.
text = " "
with open("input.txt", "r+") as f:
while text!="":
text = f.readline()
fp = f.tell()
if text == 'b\n':
print("writing at position", fp)
f.seek(fp)
f.write("-")
f.flush() #------------->
Related Topics
Elegant Python Function to Convert Camelcase to Snake_Case
Multiprocessing: Understanding Logic Behind 'Chunksize'
What Is the Quickest Way to Http Get in Python
Differencebetween Contiguous and Non-Contiguous Arrays
How to Convert a Dataframe to a Dictionary
Setting Different Color for Each Series in Scatter Plot on Matplotlib
Creating a New Column Based on If-Elif-Else Condition
How to Insert a Column at a Specific Column Index in Pandas
Choosing a File in Python with Simple Dialog
Python 3: Importerror "No Module Named Setuptools"
How to Disable Log Messages from the Requests Library
How to Connect to MySQL in Python 3 on Windows
Iterating Through Directories with Python
In Python, Differencebetween ".Append()" and "+= []"
How to Switch Position of Two Items in a Python List
Read File Data Without Saving It in Flask
What Are the Differences Between JSON and Simplejson Python Modules