5/9: what is the problem here


#1

Anyone can help? I really don't understand the warning..


#2

Please post a link to the lesson. The image helps us solve very little.


#3

I'm sorry for lack of information.
The lesson is File Input/Output: 5/9 Reading between the lines
https://www.codecademy.com/courses/python-intermediate-en-OGNHh/1/1?curriculum_id=4f89dab3d788890003000096#


#4

Stuck. Will get back to you when I've investigated further.


#5

Hi @quitapenas ,

You need to read an entire line, so use the readline method ...

print my_file.readline()

That needs to be done three times.


#6

I couldn't help with this exercise since it won't run for me:

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

SCT error message

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

I'm wondering if this is a once through type of exercise, which having been completed on an earlier date is not re-doable?


#8

I got that message from the beginning when I completed the exercise, it didn't work from the beginning and I now cannot complete the course overall because of this bug due most probably to the change between the use of output.txt earlier, to text.txt which means that the file isn't working with the code properly, if u have done what the guys appylpye said earlier

Then you have completed the exercise and no longer need to worry as long as you understand the process.


#9

@mtf

You can complete it but it requires a little wonkeyness because the server deletes the text file the user creates when they do the prior lesson. If there is a break in between sessions.

You can go to the earlier lesson and redo it for the file to get created again or you can do,

try:
    my_file = open('text.txt', 'r')
    my_file.close()
    with open("text.txt", 'r') as file:
        file.seek(0)
        for line in file:
            print(line)
except:
    with open("text.txt", 'w+') as file:
        for i in range(3):
            file.write('{} {} \n'.format('New Line', i))
        file.seek(0)
        for line in file:
            print(line)

This should allow you to pass the lesson, though I do have to say that teaching that particular method of opening and closing files is not Pythonic in the least.


#10

It makes perfect sense to simply go back and redo the lesson that creates the text file in the first place. I had to do that to complete the Twitter API module since I had to go back to it a couple of times. Don't know why I didn't think to do that again. Too busy frittering around, one supposes. I do a tremendous amount of reading and fiddling in any given day and unfortunately, retain only bits and pieces.

I love that you included the above code example. It illustrates the mechanics in a very meaningful way. Thank you. My own ignorance of things Pythonic (though I can begin to imagine) leaves me devoid of any opinion on the matter. You'll have to elucidate me on this, in your own time, and perhaps as a topic for open discussion in the Corner Bar?


#11

Only one problem... The previous lessons create 'output.txt', not 'text.txt'. Still same problem. Success using the try-except code. Thanks, again.


#12

Thanks zeziba, with your code, the program ran without a problem, but I'm sure this isn't the method intended to complete the lesson. However, without it, I couldn't advance, so I guess it's a workaround instead of a solution.


#13

The exercise is broken in that the file isn't created by the system. So you can edit the file manually, or add this:

with open('text.txt', 'w') as my_file:
    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')

or remove the curriculum_id part of the url which gets you a different submission test for this exercise which runs the above code and then asks you to submit again

The intended code does not pass the exercise as the exercise is broken.


#14

I finished my lesson.
Sorry that I didn't detail my situation at the first time, that caused much difficulty on the investigation.
After passing the lesson, I tried to reproduce the warning "Oops,try again. It looks like you didn't print the first line from the text.txt", but I failed.
So I describe my steps according to my memory as below, hope it can help.
First, I wrote below code:

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

And got this error:

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

I refreshed the page several times as requested, but still got above problem.
I tried a solution found on QA forum, that enter space then remove it in "text.txt". Still got the problem.
Then tried another solution on QA forum, changed my code as below:

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

And got the warning "Oops,try again. It looks like you didn't print the first line from the text.txt".
I repeated entering and removing space in "text.txt", still failed.
Then I rolled back the code below and it passed.

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

Sorry for any inconvenience this may cause. I would like to close this question. Thank you all for your support.


#15

I passed the lesson from

firstly wrote:
my_file = open("output.txt","r")

then modified it back to:
my_file = open("text.txt","r")

press "save & submit code". It works. Please try!


#16

I don´t think so because I am gaving the same problem. I never passed it.


#17

it doesn´t work for me, it still gives me:

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


#18

Hi Saluszavalia, may you try same process as like above ways of "quitapenas"? She passed after a "try & error". I did the same way. good luck!


#19

thanks for the help, I had the same problem, but It help me to pass the lesson.


#20

I met the same problem~


#21

Thanks for the code! I don't get it and have no idea how and why you wrote this...