python:File Input/Output


#1

hello friends, I want to know whether my code is really correct.I am getting the error message in output window but.codecademy system is allowing it
https://www.codecademy.com/courses/python-intermediate-en-OGNHh/0/3?curriculum_id=4f89dab3d788890003000096#
following is my code:

my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

# Add your code below!
for x in my_list:
    my_file.write(str(x)+'\n')
my_file.close()

This is the error message

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

#2

probably its a bug..
Just type something in your output.txt (pass a string ) file and submit the code again!
It will print all o/p! (1 ,4,9 and so on)


#3

Nope, Doesn't work.I get a new message

Oops, try again. Did you remember to close your file?

#4

Refresh the page again, type something in output.txt again and submit.


#5

Hello, I made a small change in original code.And It worked,

original`

my_file = open("output.txt", "r+")

change

my_file = open("output.txt", "w")

`
Can you tell why this happened


#6

'w' means that we're going to write on a open file.
As its a bug and created by CC editor.
though I don't have any logical explanation of all this mishappening (error).


#7

The real spooky thing is, I changed it back and the code works flawlessly.


#8

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