Writing


#1



https://www.codecademy.com/en/courses/python-intermediate-en-OGNHh/0/3?curriculum_id=4f89dab3d788890003000096


I am facing an unique issue, my code matches exactly to what people have claimed in previous queries that seems to work for them.

The console shows an error for me and the output.txt is completely blank, even then I got a message with the green tab to proceed.

I tried resetting the code and working with it again, but nothing has changed.


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

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

# Add your code below!
for i in my_list:
    my_file.write(str(i)+"\n")
    
my_file.close()    
    <img src="//codecademy-discourse.s3.amazonaws.com/original/5X/d/8/c/8/d8c8caae861ba7586c7ff0f448c41f89fc017b79.png" width="690" height="388">


#2

Hi @sandesh09dec,

Have you tried this, instead, for opening the file? ...

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

#3

Slightly off topic, does "w+" overwrite the file contents?


#4

"w+" opens the file for reading and writing. If the file does not exist, it creates the file. If it does exist, it truncates the file, so, yes, it does get overwritten.


#5

Yeah that solves the problem. Great!!
Thanks


#6