5/9 Reading between the lines: ERROR


#1

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

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

The error is in line 1 which is defined as per the instructions... I expect no error in the code... can someone please explain why the error pops up?

Thanks in advance!


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

for i in range(1,3):
    print my_file.readline()

my_file.close()


5/9 Reading between the lines. I'm getting a different error from everyone else
Getting Traceback (most recent call last): File "python", line 1, in <module> IOError: [Errno 2] No such file or directory: 'text.txt' Error
5/9 Reading between the lines. I'm getting a different error from everyone else
"No such file or directory: 'text.txt'"
5/9 Reading between the lines. I'm getting a different error from everyone else
The open() Function [Errno2]
#2

Don't use a for loop that is why they say on three seperate lines So you would write it like this

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

#3

done, error persists... infact i tried that before posting here...:confused:


#4

Write something in the text.txt window


2/9 My file is not a file?
#5

It should work after doing that :slight_smile:


5/9 Reading Between the Lines
#6

I'm the first line of the file!
I'm the second line.
Third line here, boss.

these lines are already there


#7

Change the text. And then run the code but it has to be three lines of text like this,

  1. Hi
  2. Hey
  3. Woohoo

#8

It worked now!!! :laughing::grin::sunglasses:

Thats a weird error!!!


#9

You're welcome! :grin:


#10

This worked for me too. Thanks bandit. Definitely a weird error.


#11

I tried this, and I still get the below error.

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


#12

Nevermind. I fixed it by changing "text.txt" to 'text.txt'. Weird.


#13

Just changing the text.txt file fixes everything. Weird bug in their course


#14

That worked for me, thanks!

All I did was insert one word into the first line. I didn't even delete the pre-populated text.


#15

Haha super cool. :grin:


#16

Thanks, bandit! That helped me out, too.


#17

You're welcome :grin:


#18

Writing something in the text.txt tab worked for me when nothing else did. Thank you.


#19

Just for future reference instead of looping with a traditional count controlled loop you can loop through the file like you would a string or an array like this:

my_file =  open( "text.txt", "r" )
for line in my_file:
    print( line )
my_file.close()

This way when you don't know how many lines are in the file you can still iterate through them all.


#20

Bandit, it says On three separate lines, print out the result of calling my_file.readline(). which is referring to the result that will be printed in the console, not your code. It's more efficient to use a loop here.