python - atexit not writing to file -


i'm testing see if atexit module runs attempting print file. actual purpose of atexit close open port, test if atexit working in first place. however, atexit not printing file, ideas why not? appreciated.

import atexit import scala5 scala5 import sharedvars scalavars = sharedvars() import serial myport=serial.serial()  myport.baudrate = 9600 myport.port = "com4" myport.parity=serial.parity_none myport.stopbits=serial.stopbits_one myport.bytesize=serial.eightbits myport.timeout=2 myport.writetimeout=2  try:     myport.close() except:     print("didn't need close")  def port_exit():     fo = open("test.txt", "w")     fo.write("this works!")     fo.close()  myport.open()  while true:     x = myport.readline()     if x == "1\r\n":         scalavars.door = x     scala5.scalaplayer.sleep(10)  atexit.register(port_exit) port_exit() 

you registering function after while loop, assuming exiting killing script (meaning function port_exit isn't registered). if register function before while should trigger , write file when script exits.

atexit.register(port_exit)  while true:     x = myport.readline()     if x == "1\r\n":         scalavars.door = x     scala5.scalaplayer.sleep(10) 

Comments