2
 a='aa'
>>> f=open("key.txt","w")


>>> s=str(a)
>>> f.write(s)

and still the key.txt file remains blank .. why?

2 Answers 2

10

Use

f.flush()

to flush the write to disk. Or, if you are done using f, you could use

f.close()

to flush and close the file.

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

Comments

2

This issue can be avoided completely by making use of the with statement:

with open("key.txt","w") as f:
    s=str(a)
    f.write(s)

The file will be automatically closed when the block completes. Using the with statement you need not worry about this sort of bug creeping into your code.

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.