Python linecache is not working

2.5k Views Asked by At

In Python, I have an issue where whenever I use the getline() function from linecache module, it won't work at all. Say this was what I had on a text document named hi.txt:

Hi

And say this is what I had on a python program in the same folder/directory:

import linecache

print (linecache.getline("hi.txt", 0))

It would print nothing, just some blank lines of nothing.

1

There are 1 best solutions below

2
Jean-François Fabre On BEST ANSWER

linecache.getline starts at 1.

print (linecache.getline("hi.txt", 1))

does what you expect

>>> help(linecache.getline)
> getline(filename, lineno, module_globals=None)

by convention lineno starts at 1 in any text editor.