"Writing" exercise passes, though produces error in console window and fails to populate ouput.txt


#1

I've been messing with this for a while now, and have attempted to troubleshoot this any way I can based upon posts made by other users. This includes editing my code, refreshing the lesson, resetting the code, and trying to get the code to work in both Firefox and Chrome.

As far as I know, my code is exactly how it's supposed to be:

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

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

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

And, although I pass the lesson, this error appears in the console window:

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

If I could run the script on my own computer, I would, but I can't seem to get python scripts from Notepad++ to work properly on my computer (this is a separate issue altogether. I am hoping that there is a way around this, but sense that it is a bug and may just have to move on to the next lesson. Any help is greatly appreciated.


#3

Notepad++ is an editor, it does not execute python code. You need a python interpreter which you would run with a filename as an argument:

$ python mycode.py

And yes, codecademy broke file handling somewhat in that course


#4

Perhaps I should have been more clear. I understand that Notepad++ is an editor. I have Python 3.5.2 installed, how to I get my scripts to run through that? Right now whenever I hit the "Run . . ." option in Notepad++ it just bring up the command line for a split second and then immediately closes, for what that information is worth. I understand I should be going through the command line to run scripts, but as I said I am new to programming and haven't quite tackled that yet. I'm using an old Toshiba laptop running Windows 7 right now, as that's the only computer I own.


#5

I know little about windows-only editors, but I expect that the best place to start is with a google search anyway. If as you say a cmd window pops up, perhaps it is running.


#6

Well windows doesnt exactly have an environment where you can run command line unlike macs and linux. I suggest just downloading an IDE. I used Wing IDE but there are others out there for windows.

Have you tried doing something to your output.txt? Such as remove a period or a character and add it back again?


#7

Yeah, my guess is that it runs in the command window very quickly, and then closes as soon as it's done running.


#8

I'll look into Wing, thanks. Just tried deleting the word "boss" and typed it back in again, and this error message came up:

  File "python", line 1
    I'm the first line of the file!
                                  ^
SyntaxError: EOL while scanning string literal

#9

That doesn't look like something you should be trying to execute, it's data, not code


#10

I'm not trying to execute it, that's what comes up in the console window when I attempt to execute it.


#11

Gonna have to re-think that.


#12

I miswrote, this error message is what comes up when I attempt to execute my code.


#13

But it's telling you that there was an error while trying to execute that line, which isn't code. Wrong file? Wrong file content?


#14

Ohhh wait, I've confused this for another exercise! So sorry!


#15

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