Reputation: 476
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.
Upvotes: 3
Views: 23229
Reputation: 1
I think maybe the story is like this:
file.readline()
but it didn't work as expectedfile.readline()
and then you correct your code also as file.readline()file.readline()
will be executed firstly from your watch and then from your source code.That's why your source code's file.readline()
cannot get anything.
All in all, not all operation is idempotent, your debug watch may be interfering your source codes' running...
Upvotes: -1
Reputation: 476
The issue was that when scripting I had to use print
, but in a command prompt, I can just use f.readlines()
and it will read the lines.
Upvotes: -3
Reputation: 1
I got the same problem. That happens because Python is not showing multiple outputs but just one. Import the following and that will solve the problem.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
Upvotes: 0
Reputation: 61
I've figured out an answer to a very similar issue in case someone comes here looking. So in Python if you have ran code of readline()
prior to readlines()
then it has already iterated through those lines.
Try exiting out and ONLY typing readlines()
for a file and it will make the full list for you.
Upvotes: 6
Reputation: 19
f.readlines() returns all the lines at once inside a list, for your example:
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']
as for f.readline() will return the lines one by one starting from where the cursor is untill the end of the file, your example output will be:
f=open('test.txt')
f.readline()
'Hello This Is Test\n'
f.readline()
'For Loops In Files\n'
f.readline()
'Flying Away\n'
f.readline()
'A Bird also Flies\n'
f.readline()
'Wow'
f.readline()
''#empty string
to set the cursor to the beginning of the file use seek(0)
Upvotes: 1
Reputation: 3883
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()
Upvotes: 1