Reading between lines


#1

error :slight_smile:Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is save
code:
my_file = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#2

this exercise experiences some problems, here is a solution:


#3

ok thanks a lot .................


#4

Still doesn't work :confused:

Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved.

File "python", line 1
file f = open("text.txt", "w")
^
SyntaxError: invalid syntax

file f = open("text.txt", "w")
f.write("Hello World!" + "\n")
f.write("Hello World!!" + "\n")
f.write("Hello World!!!" + "\n")
f.close()

my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#5

why file f? f is simply a variable you want to store the result of opening a file in. I don't see why you need to use file


#6

oh thank you, previously I changed it with "my_file" but didn't remove "file" just "f", now it works :slight_smile:


#7

my_file, f, you have the freedom to name this variable to your liking (okay, maybe not always in the codecademy lessons), and you should still give it a logic name


#8

this worked for me:

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

my_file = open("text.txt", "r")

print my_file.readline()
print my_file.readline()
print my_file.readline()

my_file.close()

but is there a place to flag this lesson for review so this issue can be fixed?
or will it be reviewed now since this has been mentioned on the forum?

thanks!


#9

It seems to be a useful bug:slight_smile: It is relatively easy to guess the solution. If file doesn't exist we have to create it.


#10