Ask coding questions

← Back to all posts
Python not writing to file
LordTopley

So, I'm learning from scratch and I've just begun with Writing to files.

Here is my code:

my_file = open('out.txt', 'w')
my_file.write('Hello World')

I see the out.txt is created, but nothing is written to the file

Voters
johmon6
LordTopley
Comments
hotnewtop
LordTopley

I worked it out.

I was forgetting to the close() the file after writing.

For those finding the same issue, this is the corrected code

my_file = open('out.txt', 'w')
my_file.write('Hello World')
my_file.close()

AbuAlAsif1

Thanks @LordTopley, I was having the exact same problem.

gozie11

@LordTopley I was having the same problem just because i forgot the parentheses at the end of file.close()