5

Below is my code, I am using :

with open(r'C:\Users\Manish\Desktop\File5.txt', 'r') as f:
     fo = f.read(20)
     print(fo)
     f.seek(20,1)
     fo = f.read(20)
     print(fo)

But instead of getting next lines from current position, it repeatedly showing me error. Where is the problem in my code ?

snakecharmerb
  • 36,887
  • 10
  • 71
  • 115
Manish Pal
  • 256
  • 6
  • 18

2 Answers2

4

It seems like offset from current stream and end of stream only supported in binary mode. Which you have to open the file with

open(r'C:\Users\Manish\Desktop\File5.txt', 'rb')
MaxV
  • 2,358
  • 3
  • 16
  • 22
Xenox Inno
  • 88
  • 5
0

Syntax:

f.seek(offset, from_what), where f is file pointer

Parameters:

Offset: Number of positions to move forward from_what: It defines point of reference.

Returns:

Does not return any value

The reference point is selected by the from_what argument. It accepts three values:

0: sets the reference point at the beginning of the file

1: sets the reference point at the current file position

2: sets the reference point at the end of the file

By default from_what argument is set to 0. Note: Reference point at current position / end of file cannot be set in text mode except when offset is equal to 0.

Glauco
  • 1,097
  • 2
  • 8
  • 18
  • It looks okay but it might be too educative, I think it would be best to point out what the error is, and then educate. In that answer there is no clear "answer" to the problem, only documentation – Nathan Marotte Nov 09 '21 at 18:01