python:File Input/Output


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
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:

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'


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)


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

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


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


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


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


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

Can you tell why this happened


'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).


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


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