Search code examples
pythonfiletextreturnreturn-value

f.readlines() won't return any values


So I was reading over the Python Documentation/Tutorial, specifically I was looking at the Input/Output with files. This is what the tutorial says will return whatevers in the file:

>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''

I put the same thing in my .py and it didn't return any exceptions or issues, but it didn't return any values, either. This is my code:

f = open('test.txt')

f.readlines()

Unless I use the variables and a print, the program will not return anything thats in the file. Heres my files contents:

Hello This Is Test
For Loops In Files
Flying Away
A Bird also Flies
Wow

Any ideas as to why this isn't working? I think its my interpreter, as I am using SublimeREPL, but it hasn't been doing anything weird before then so I am not too sure. Also, when I try running it in my Python console, it gives me an error, then just closes the console so I can't even see the error. Pretty counter-productive.

Just to clarify, my issue is not the for loop, the for loop already worked fine from before. The issue is that f.readlines() does not return any lines, even though the documentation says it should return the next line. I will take the for loop out of my code to clarify. Also, sorry if I am mixing up f.readline() and f.readlines(), but they both don't work, anyway.


Solution

  • If you want to read a file line by line, this would be a better option. The 'for line in f:' iterates over the file as a list. The 'line' variable includes the trailing '\n' or '\r\n', so you'll want to strip those.

    The 'with' statement ensures that the file is closed even if your program throws an exception when reading the file.

    with open('test.txt','r') as f:
        for line in f:
            print(line.rstrip('\n'))
    

    Output:

    $ python test.py
    Hello This Is Test
    For Loops In Files
    Flying Away
    A Bird also Flies
    Wow
    

    Using readlines:

    open('test.txt','r') as f:
        lines = f.readlines()
    
    for line in lines:
        print(line.rstrip('\n'))
    

    readlines turns the file into a list in memory. It isn't efficient if you have to read a large file.

    Using both:

    with open('test.txt','r') as f:
        line = f.readline()
        print(line.rstrip('\n'))
        print
        lines = f.readlines()
    
    for line in lines:
        print(line.rstrip('\n'))
    

    Output:

    $ python test.py
    Hello This Is Test
    
    For Loops In Files
    Flying Away
    A Bird also Flies
    Wow
    

    From python console:

    $ python
    Python 2.7.8 (default, Jul 28 2014, 01:34:03)
    [GCC 4.8.3] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f = open('test.txt')
    >>> f.readlines()
    ['Hello This Is Test\n', 'For Loops In Files\n', 'Flying Away\n', 'A Bird also Flies\n', 'Wow\n', '\n']
    >>> f.close()