Negative seeking python

python seek
python binary seek
python file seek line
can't do nonzero cur-relative seeks
python file seek exception
python file seek slow
python seek string in file
python seek invalid argument

So I'm trying to read the second to last number in a file. My code is as follows:

file = open("file.txt", "rb")
print (file.seek(-2, 2))
file.close()

The contents of my file are as follows:

1,2,1,1,2,0,1,1,2,1,1,1,0,0,0,0,1,2,0,0,0,1,0,1,1,0,0,0,1,0,0,2,1,1,

I expected the program to print "1" but it actually prints "3" Does anyone know why this is and how to fix it? Thanks!


It's worth noting that seek operates by an offset of characters (or bytes in binary mode). Each comma separating the numbers in your example is going to count as a character (or byte in your case since you did open in binary mode). In order to read the second to last number in the example you provided, you would need to seek back 4 characters (2 numbers and 2 commas, since your file ends in a comma), then read a single character

f.seek(-4, 2)
r.read(1)

Realize that this won't work if any of the numbers are larger than 1 digit. Looking at your file, it doesn't appear to be written in binary, so opening it in binary mode doesn't make much sense. If the file is small enough to fit into memory, it would be simpler and less error prone to read the whole file, then parse it to get the second to last number

number = int(f.read().strip(',').split(',')[-2])

Issue 3524: IOError when attempting negative seek in file (Python , example of seeking in a negative direction from the end of a file. I get an IOError when attempting to run the example in Python 3.0b2 on Mac  Python Program to Check if a Number is Positive, Negative or 0 Python Program to Check if a Number is Positive, Negative or 0 In this example, you will learn to check whether a number entered by the user is positive, negative or zero. This problem is solved using ifelifelse and nested ifelse statement.


The return value of seek is not anything read from the file. On Python 2, the return value is None; on Python 3, the return value is a number representing the new absolute position in the file, which may not correspond to the number of bytes or characters from the start of the file in text mode.

If you want to read a character after seeking, call file.read(1).

Negative Seek Offset in Python 3.5 · Issue #30 · jcrobak/parquet , in to a problem. Evidently, Python 3.5 no longer supports negative seek offsets. When I try to open a simple file, I get the following exception:. In any other language that does not support negative indexing, one will have to write. to achieve the same effect. Python being a beautiful language, provides this negative indexing concept which makes the programmers write less and the code more readable.


Another way is to read the file and split it into array. Finally fetch the last but one field of array as follows.

if __name__ == '__main__':
    file = open("file.txt", "rb")
    st = file.read().decode('utf-8').split(',') #convert into array
    l = len(st) # length of the array
    print(st[l - 2]) #(n-2) th element of the array
    file.close()

offset can not be negative in File.seek()?, The negative seek works if the file is opened in binary mode 'rb'. (Sep-27-2019, 07:01 PM)Gribouillis Wrote: The negative seek works if the file is opened in binary mode 'rb'. I've used your library successfully in Python 2.7, but when I tried it on Python 3.5, I ran in to a problem. Evidently, Python 3.5 no longer supports negative seek offsets. When I try to open a


Python: seek, A positive offset will move the pointer forward, a negative offset would move backward. Special cases. There are two special cases: fh.seek(0, os.SEEK_SET) - go  For negative index, -n is the first index, - (n-1) second, last negative index will be – 1. - A negative index accesses elements from the end of the list counting backwards. - An example to show negative index in python. >>> import array. >>> a= [1, 2, 3] >>> print a [-3] 1. >>> print a [-2] 2.


Python 3 - File seek() Method, Python 3 - File seek() Method - The method seek() sets the file's current position at the offset. The whence argument is optional and defaults to 0, which means  How to do Negative Binomial Regression in Python STEP 1: We will now configure and fit the Poisson regression model on the training data set. Setup the regression STEP 2: We will now fit the auxiliary OLS regression model on the data set and use the fitted model to get the value of STEP 3: We


Python seek() function, Example 2: Seek() function with negative offset only works when file is opened in binary mode. Let's suppose the binary file contains the following text. b'Code is  Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side). so if you have a list as so: myList = ['a', 'b', 'c', 'd', 'e'] print myList[-1] # prints 'e' the print statement will print "e".