1

I am trying to write a basic file writing syntax inside a function but it doesn't work. The file is never created. When i do it outside function, it works fine. I am not sure what I am doing wrong. Kindly point me in the right direction

def write():
    file=open("testfle.txt","w")
    file.write('hello')
    file.close()

    return

2 Answers 2

2

I saved your code in a file named create.py in IDLE and then ran it by calling your method write(), it works when you call the method write() like below.

It created a file named testfle.txt with content as hello

def write():
    file=open("testfle.txt","w")
    file.write('hello')
    file.close()
    return

## call write
write()

Also be very watchful of indentation when working with Python :).

Sign up to request clarification or add additional context in comments.

1 Comment

np, happens a lot often then we think :)!. Please accept the answer and vote up!
0

Could be you mistyped "testfile.txt" as "testfle.txt".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.