Search code examples
pythonpython-3.xlinecache

Python linecache is not working


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.


Solution

  • 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.