Skip to main content
The 2024 Developer Survey results are live! See the results
edited tags
Link
Boann
  • 49.5k
  • 16
  • 121
  • 150
Source Link
user1517644
user1517644

Method of exiting and closing file for both Windows + Linux

At the moment I'm using this code

if __name__=="__main__":
    try:
        main()
    except KeyboardInterrupt:
        f.close
        print "left!"

Is that the best way to do it? Earlier on in the script a file is being written to and I want to make sure its closed cleanly should the script be terminated. Any views please?