5. Reading Between the lines


#1



https://www.codecademy.com/courses/python-intermediate-en-OGNHh/1/1?curriculum_id=4f89dab3d788890003000096#


it prints this out:
I
'm
th


This should be printed out:
I'm the first line of the file!
I'm the second line.
Third line here, boss.

my_file = open("text.txt", 'r')
print my_file.readline(1)
print my_file.readline(2)
print my_file.readline(3)
my_file.close()


#2

That's because you have numbers in your print my_file.readline()s. In order to read the whole line, you don't need the numbers.


#3

Is it my mistake, or is it a glitch?
Browser reboot doesnt help.


#4

Same problem here! So far I didn't find a solution.

edit:
executing the following code should give the files (and folders) in the current working directory. In my case it only shows the file 'script.py' and because 'text.txt' is missing, I think it cannot be opened with open().

import os
print(os.listdir(os.getcwd()))

For the excercise maybe (if possible) try to create such a file first and then opening it and using readline().

edit2:
We're not the first ones, see here (with "solution"):


#5

Yeah, it works. Thanks!


#6

I am also getting the same issue!


#7

Thank you, it works)


#9

use this code, I have done it...
my_file=open("text.txt","r+")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#10

More easy... go to "output.txt" tab, press on last line and them click in "Save & Submit Code". This will write this file on disk and let you finish the exercise.


#11

Though when you go to text.txt file it is showing some content, but in the database the file is empty. So you need to write some line in the text.txt file and the read it out!
I tried the code below and it worked!

my_file = open("text.txt", "w")
my_file.write("I'm the first line of the file!\n")
my_file.write("I'm the second line.\n")
my_file.write("Third line here, boss.\n")
my_file.close()
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.