Python Forum
offset can not be negative in File.seek()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
offset can not be negative in File.seek()?
#3
With the right whence-flag, you can use negative numbers.
Look here: https://docs.python.org/3/library/io.htm...OBase.seek

In [1]: from pathlib import Path                                                              

In [2]: raspbian = Path('Downloads/2019-07-10-raspbian-buster-lite.zip')                      

In [3]: with raspbian.open('rb') as fd: 
   ...:     print('Size:', raspbian.stat().st_size) 
   ...:     print('Pos:', fd.tell()) 
   ...:     print('Seek to -10 bytes from end') 
   ...:     fd.seek(-10, 2) 
   ...:     print('New position:', fd.tell()) 
   ...:                                                                                       
Size: 426250971
Pos: 0
Seek to -10 bytes from end
New position: 426250961
Instead of recognizing the number for 'whence', you can use os.SEEK_END:
In [10]: print(os.SEEK_SET, os.SEEK_CUR, os.SEEK_END)                                         
0 1 2

In addition, I've used pathlib.Path, which is better to handle.
You can still use the built-in function open().
The resulting file-object is the same as from Path.open(mode).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: offset can not be negative in File.seek()? - by DeaD_EyE - Sep-27-2019, 12:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between tell() and seek(0, 1) tekberg 2 1,491 Oct-11-2024, 05:00 AM
Last Post: deanhystad
  negative memory usage akbarza 1 1,264 Apr-27-2024, 08:43 AM
Last Post: Gribouillis
  Goal Seek tdutcher05 1 3,032 Nov-17-2023, 10:33 PM
Last Post: deanhystad
  is there any tool to convert negative base to int? Skaperen 7 3,691 May-27-2022, 07:30 AM
Last Post: Gribouillis
  pytz: get integer utc offset from Olsen region/city h4tt3n 2 8,672 Jul-30-2020, 06:43 AM
Last Post: h4tt3n
  how to write offset number to disk use python? Pyguys 4 4,242 Apr-11-2020, 07:53 AM
Last Post: Pyguys
  search binary file and list all founded keyword offset Pyguys 4 4,277 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Def code does not work for negative integers. doug2019 1 2,593 Oct-31-2019, 11:00 PM
Last Post: ichabod801
  Positive to negative bernardoB 6 5,743 Mar-13-2019, 07:39 PM
Last Post: bernardoB
  Python offset to length write in a File basic_learner 3 5,569 Dec-13-2018, 06:20 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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