Python Forum
Difference between tell() and seek(0, 1)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between tell() and seek(0, 1)
#1
I have a binary file being read from, but unfortunately the file object has no tell method.

As an example, assume the following shell command is run:

echo "abcdefghijklmnopqrstuvxyz" > foo.txt

The following are python commands and their output:

>>> f = open('foo.txt', 'rb')
>>> f.read(3)
b'abc'
>>> f.tell()
3
>>> f.seek(0, 1)
3
>>> f.read()
b'defghijklmnopqrstuvxyz\n'
>>> f.close()

If looks like tell() and seek(0, 1) return the same value. Am I missing something, or am I spot on?

The reason for this is my use of the wav module while trying to concatenate .wav files. When I get to the point were it calls the tell() function the following message is displayed:
AttributeError: 'WindowsPath' object has no attribute 'tell'
I can override the method that that package's calls to tell() to instead call a method that returns seel(0, 1).
Reply
#2
First of all, the package I am using is wave, not wav. It is used to read and write .wav files. The file argument I was passing to wave.open was a Path object from pathlib. The wave code assumes that if the first argument to wave.open is not a string that it is a file pointer. My solution was to convert the Path object to a string, and undoing my patch. It works find now.
Gribouillis likes this post
Reply
#3
tell() tells you the current file position. seek() sets the current file position and returns the new position. They will return the same file value if you seek() the position returned by tell(). seek(0, 1) will set the file position to the current position (0 offset from the current file position).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Goal Seek tdutcher05 1 3,071 Nov-17-2023, 10:33 PM
Last Post: deanhystad
  offset can not be negative in File.seek()? jollydragon 6 9,200 Sep-28-2019, 03:08 AM
Last Post: jollydragon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020