-1

How do I make it so that python automatically closes open files when the player attempts to quit?

This is what I have so far:

hai = open('hai.txt','a')
quit = 0
while quit == 0:
    hai.write('SPAM')
hai.close()
NightShadeQueen
  • 2,946
  • 3
  • 19
  • 35
Pop Car
  • 349
  • 3
  • 12
  • 1
    In most cases, even without using the 'with' statement, python will take of that for you. http://blog.lerner.co.il/dont-use-python-close-files-answer-depends/ – Yotam Vaknin Sep 01 '15 at 19:36

1 Answers1

1

Use

with open("file.txt","a") as f:
    #Do things with f
YOBA
  • 2,459
  • 12
  • 27