How to detect when the user closes a file?


I am trying to create a Python program where the user can input text into a file. After they close and save the file, I want to print its content. How do I detect when the user has saved and closed the file?

This is the code I have used to open the text file.

def opentextbox():
    login = os.getlogin()
    file1 = open('C:/Users/'+login+'/enteryourmessagehere.txt', 'a')
    file1.close()['notepad.exe', 'C:/Users/'+login+'/enteryourmessagehere.txt'])


You can use multi-threading here. Create a thread, something like this:

import threading
thread1 = threading.Thread(target=function[, args=arguments])

Where function can be something like this:

import time
def function(file_handle):
  while 1:
    time.sleep(2) # Put your time in seconds accordingly
    if file_handle.closed:
      print "User closed the file"

And run this thread in the background while your main function goes on.

Or you can just create another thread, if you will, and put the rest of your code there, run the two threads simultaneously and you're done.