5.Reading Between the Lines help me please


#1



5.Reading Between the Lines


i keep getting those errors since forever

Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt'

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


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


#2

just found the solution
add a 4th line to text.txt


#3

I am getting the exact same error (for two days now), while attempting to complete this lesson. My code is identical to yours.


#4

I tried adding a fourth line to text.txt and I am still getting the same error.


#5

@burntpriest and @redanaeem - I also had the same problem...

When I changed the code to list the files in the current directory:

import os
print os.listdir(".")

The output was:

['script.py']

So it seemed as if the required text.txt file didn't exist.

So I wrote the code to create/write the text.txt file so that it did exist:

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()

Then when I checked the current directory contents using the above code again, i got:

['text.txt', 'script.py']

Now when I write the code to solve the exercise, it works OK.


#6

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

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


First block is medicine, second - exercise.


#7

this is too good bro


#8

add a new line in the text.txt file that worked for me.


#9

Thanks for your solution @redanaeem, It works perfectly :slight_smile:


#10

I just added " " around each of the lines and ended up getting it to work, maybe that will help


#11