-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
  • 3,284
  • 3
  • 24
  • 37
Pop Car
  • 363
  • 5
  • 15
  • 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,759
  • 1
  • 14
  • 29