2/9 Does not recognize 'output.txt'


#1

When I hit 'Save and Submit', it returns:
Traceback (most recent call last):
File "python", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'output.txt'

and Oops, try again. Did you create a variable called my_file?
My code:
my_file = open("output.txt", "r+")


#2

Get into this habbit when looking for a file. They need to add this little bit to the course because it is not common sense but something you learn.

CODE

try:
    print("Attempting to open file")
    file  = open("some_txt_file.txt", "r")
    print("File Opened")
except IOError:
    print("Failed to load file!")
    file = open("some_txt_file.txt", "w+")
    print("File Created!")

#3

Click on the output.txt tab and change something inside file. For example: press "Enter" at the first line in the file. Then execute your code again. :smile:


#4

Does output.txt have anything in it beforehand? Here's my code: my_file = open("output.txt", "r+") It seems to be the same as yours...maybe restart your browser or refresh the page.


#5

Your code is fine, I tried it and it works.


#6

that worked for me but begs the question, why?
thanks!


#7

i prefer this module to try/except blocks tbh


#8

I can see why. Keep up the good work!


#12

That did it. Actually had to poke in and out of the "text.txt" file a couple of times, then it worked.


#13

\\\\\\\\\\\Thanks!////////////////////////////