I accidently used `.readlines()`, but why does it look different than in the last exercise?

If I write the solution with .readlines() instead of .readline(), the output looks like a list, with \n s. It reads:

[‘You do look, my son, in a moved sort,\n’, ‘As if you were dismayd: be cheerful, sir.\n’, ‘Our revels now are ended. These our actors,\n’, ‘As I foretold you, were all spirits and\n’, ‘Are melted into air, into thin air:\n’, ‘And, like the baseless fabric of this vision,\n’, ‘The cloud-cappd towers, the gorgeous palaces,\n’, ‘The solemn temples, the great globe itself,\n’, ‘Ye all which it inherit, shall dissolve\n’, ‘And, like this insubstantial pageant faded,\n’, ‘Leave not a rack behind. We are such stuff\n’, ‘As dreams are made on, and our little life\n’, ‘Is rounded with a sleep. Sir, I am vexd;\n’, ‘Bear with my weakness; my, brain is troubled:\n’, ‘Be not disturbd with my infirmity:\n’, ‘If you be pleased, retire into my cell\n’, “And there repose: a turn or two I’ll walk,\n”, ‘To still my beating mind.\n’]
[“Austin runs this show 'round here.”]

In the previous exercise, I used .readlines(), and there were no square brackets or \n s in the output. Why is this?

2 Likes

Hey,

I noticed this too, looked it up and tried it on the previous exercises (1/13, 2/13).

Here, https://docs.python.org/2/library/stdtypes.html?highlight=.readline#file.readline, it says:

Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). 6 If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When size is not 0, an empty string is returned only when EOF is encountered immediately.

Here, https://www.w3schools.com/python/ref_file_readlines.asp, it says:
The readlines() method returns a list containing each line in the file as a list item.

If you try 1/13, it also returns a list after you use .readlines(). For 2/13, I’m not too sure why it does return a list, but my guess is because each of the lines did not end with a fullstop or comma, and that the next line in 2 lines after.

If anyone else can shed the light on this, it will be most appreciated!

5 Likes

What exactly is the question? .readline() and .readlines() are two different methods, where .readline() returns the next line, and .readlines() returns all lines.

19 Likes

Hey stetim94,

I think the other learner’s is confused why using .readlines() will output the whole file as a list, and each new line of the file as an item of the list.

e.g.:
[‘You do look, my son, in a moved sort,\n’, ‘As if you were dismayd: be cheerful, sir.\n’, ‘Our revels now are ended. These our actors,\n’, ‘As I foretold you, were all spirits and\n’, ‘Are melted into air, into thin air:\n’, ‘And, like the baseless fabric of this vision,\n’, ‘The cloud-cappd towers, the gorgeous palaces,\n’, ‘The solemn temples, the great globe itself,\n’, ‘Ye all which it inherit, shall dissolve\n’, ‘And, like this insubstantial pageant faded,\n’, ‘Leave not a rack behind. We are such stuff\n’, ‘As dreams are made on, and our little life\n’, ‘Is rounded with a sleep. Sir, I am vexd;\n’, ‘Bear with my weakness; my, brain is troubled:\n’, ‘Be not disturbd with my infirmity:\n’, ‘If you be pleased, retire into my cell\n’, “And there repose: a turn or two I’ll walk,\n”, ‘To still my beating mind.\n’]

Instead of what will be printed, if .read() was used:
You do look, my son, in a moved sort,
As if you were dismayd: be cheerful, sir.
Our revels now are ended. These our actors,
As I foretold you, were all spirits and
Are melted into air, into thin air:
And, like the baseless fabric of this vision,
The cloud-cappd towers, the gorgeous palaces,
The solemn temples, the great globe itself,
Ye all which it inherit, shall dissolve
And, like this insubstantial pageant faded,
Leave not a rack behind. We are such stuff
As dreams are made on, and our little life
Is rounded with a sleep. Sir, I am vexd;
Bear with my weakness; my, brain is troubled:
Be not disturbd with my infirmity:
If you be pleased, retire into my cell
And there repose: a turn or two I’ll walk,
To still my beating mind.

5 Likes

that is 8 months ago, unlikely he will come back to this topic. Do you have a question?

i think the confusion is here:

but without code is impossible to verify what exactly he did, .readlines should output a list (unless used in a loop i suppose), i think he use .readline in the previous exercise, easy to confuse these methods, but difficult to determine with absolute certainty.

5 Likes

Hey again, nope I don’t have a question. Usually as I learn each part, I just check the forum for insights, sometimes I learn more things from the questions people ask.

I initially didn’t know that .readlines() produced a list until I checked it on Google. I know the guy’s isn’t likely to come back to this topic, but possible clarification to his post may help another curious learner out in the future.

He is likely referring to exercise 2/13, because I did .readlines() on the text file and it didn’t return a list.

35 Likes

then it crashed.
or you called something else.
or it was a list.

no other options.

the whole purpose of that method is to return lines ← you can tell from the name already without even reading its documentation
or if you do read the documentation, you can see that it does return a list and nothing else

and, in response to your earlier post, periods and commas do not relate to lines in any way, so that won’t affect what is considered a line. that would be incredibly surprising in a bad way.

1 Like
['1. How many lines do we write on the daily,\n', '2. Many money, we write many many many\n', '3. How many lines do you write on the daily,\n', '4. Say you say many money, you write many many many\n']

I stand corrected. I went back to check, now I understand the previous learner’s and my mistake.
We wrote this:

... for line in xxx.readlines(): 
print line

Instead of this:

 line =  xxx.readlines()
 print(line)
12 Likes

i covered it:

readlines still produce a list, but because a loop is used you never “see” the list. mystery solved.

9 Likes

Yep, the loop made the list “invisible”, mystery solved!

7 Likes

don’t think of it as text, it’s structures of data, and those structures support various operations

the brackets spaces and commas you see when printing a list is not part of the list in any way, that is a text representation that is created by extracting values from a list, invoking their tostring code and adding it to the current string being built until the process is complete at which point you have a completely different data structure containing text, that’s no longer a list, that’s just a bunch of characters.

At hardware level you’ve got 1/0 cells in groups of 8
you might represent a character with such a group, that would give you 256 different values (2 ** 8 combinations)
if you take many such groups in a row that could represent a string

now you’d have a string somewhere in memory at a certain location with a certain length
to refer to it you would use the address to that location, that’s an integer
to make a list you might put such integers one immediately after the other. your list is now just numbers, no braces or anything of the like.

to read that list, you’d start at its beginning location, and read enough bits that you have the size of the addressing integer. then you would visit that location, and read the characters there until you reach the end (and somehow you’d have to represent the length or the end), then you’d go back to the list, read the next integer, visit that location, and so on.
meanwhile you might be storing the results in a new location, maybe you’re building up a string representation, that would be another series of groups of 8 bits that you would be adding onto as you went through the list

then you’d have to tell the print procedure about the location of the string you just made

and printing in this case really only means writing those bits to file because your program does not control the monitor or any other display device. often that file is also open by another program such as your terminal application which would in turn generate a configuration of drawing instructions or maybe a complete image to be moved into the memory of your graphics card and that would do its thing and eventually pass information along to the monitor

14 Likes

that is the way python seperates stuff into different lines

yes, you’re right! this did help me in the future thanks for clarifying

1 Like

Same. I’m new to coding, and I wasn’t thinking about the loop part of the previous exercise, so I would have expected it to print out similarly to that previous exercise, too, had I used readlines() instead of readline().

I think it was because in the previous exercise a loop was used.

.readlines() creates a list, in square brackets, with each line as an element of the list.

In the previous exercise we ran a loop for .readlines() saying:

for lines in …readlines():
print (lines)

so each line in the list was printed onto a new line in the output, as the code iterated through the list. If we had not used a loop and just used .readlines() as you have accidentally done here, we would have seen a list in square brackets.

If I’m not mistaken?

Yes, it’s mentioned above but .readlines() always returns a list. The square brackets are just the standard representation of lists as a string so if you passed the list object to print then that’s what you’d see. The docs state list(fileobj)- py3docs-reading and writing files could be used instead if that makes the list more obvious.

2 Likes

Another behavior I don’t understand : I tried reopening the file with the following code, but I threw a ValueError: I/O operation on closed file at me. Can somebody explain why? Thanks!

with open("just_the_first.txt") as first_line_doc:
  first_line = first_line_doc.readline()
  print(first_line)
  second_line = first_line_doc.readline()
  print(second_line)

with open("just_the_first.txt") as test:
  testtest = first_line_doc.readline()

It’s a slight simplification but your code runs much like the following would-

f1 = open("just_the_first.txt", "r")
fo.close()
f2 = open("just_the_first.txt", "r")
f1.readline()  # using a file object that has been closed

You can basically ignore the middle line in this example since it doesn’t change the outcome, you close first_line_doc so attempting further reads does not work (as the error mentions). For files at least there’s nothing stopping you creating multiple file objects directed to the same file though it might be wise not to do that, they’re not the same object.

Thanks for you answer!

So if I understand well, each time I want to open a file as a temporary object (like, in your example, f1 and f2), I need to do it as often as needed because it will be closed automatically?

Second line in that previous message has a typo and should be f1.close() just in case it wasn’t clear.

Using with will close it for you at the end of the indented suite of code, it’s often suggested since it closes the whole thing neatly regardless of errors. It is the suggested method of working with files. There’s nothing explicity requiring you to use with but failing to close a file after opening it is, at best, a waste of resources and can often lead to unexpected behaviour. You’ll want to make sure they’re closed.

So yes, you certianly need your file open before you perform any I/O operations on its contents but you can cut down on the repetition of opening the file yourself using loops, functions and otherwise in whatever way suits best. You could also create your own context manager that can be used in conjunction with the with statement should you need more complex behaviour.

Some useful links with more info than I can hope to pass along-
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

4 Likes