python - Method of exiting and closing file for both Windows + Linux -
I am currently using this code
if __name __ == "__ Main__ ": Try: Keyboard Interrupts except the main interface (): f.close print" left! " Is this the best way to do this? Earlier, a file is being written in the script and I want to ensure that the script has to be closed. Any ideas please?
Note: f.close is not actually close the file , You have to call the function: f.close () To answer your question, using the best method with the block Used to be . The file will close automatically, even if an exception has been raised: with open ('test.txt') f: pass # pass = Close the file automatically with block exits
Comments
Post a Comment