My function cannot create text file

I am a beginner in Python and i am reading Wrox's "Beginning Python Using Python 2.6 and Python 3.1"... There is one certain example in chapter 8 about using files and directories that has troubled me a lot... The following function is supposed to create (if it doesn't exist) and write in a text file:

def write_to_file():
f=open("C:/Python33/test.txt","w")
f.write("TEST TEST TEST TEST")
f.close()

When i run the function nothing happens, no text file is created and no error message is returned...

When i run the code in IDLE, command by command, it works perfectly...

What is wrong with the function???

Upvotes: 1

Views: 248

Answers (2)

A human being
A human being

Reputation: 1220

I think this is because of indentation, do it like this:

def write_to_file():
    f=open("C:/Python33/test.txt","w")
    f.write("TEST TEST TEST TEST")
    f.close()

Upvotes: 0

Xenolithic
Xenolithic

Reputation: 210

Python's picky about indentation, from what I remember of it:

def write_to_file():
    f = open("C:/Python33/test.txt", "w")
    f.write("TEST TEST TEST TEST")
    f.close()

# On top of that, you need to actually run the function.
write_to_file()

Upvotes: 1

Related Questions