Detecting broken stream in python when deleting a file

My problem is that logging stops for the python program when the log rotates. I tracked it all the way to the stream. I see no way to determine if the thread has been disconnected from python. After deleting the file, it still takes notes without any problems.

import os FILE = 'testing.txt' fs = open(FILE, 'a') fs.write('word') os.remove(FILE) fs.write('Nothing....') # Nothing breaks print(fs.errors) # No errors 

So how can I find out if a file stream is being saved? And checking if a file exists will not help, since the file will always exist regardless of whether the stream is valid.

+4
source share
4 answers

After much more inspection, I found a solution. This is a specific OS problem. When a file is deleted on Linux (or Macintosh), it simply disables it. (I did not know about this) Therefore, if you run lsof on the machine, it still shows the file as open.

 [ user@machine ]$ lsof | grep --color -i "testing.txt" python26 26495 user 8w REG 8,33 23474 671920 /home/user/temp/testing.txt (deleted) 

The solution is to install the stream in python.

 stat = os.fstat(fs.fileno()) 

This will give you the number of links that it has.

 if stat.st_nlink < 1: #has been deleted 

And you go there. Now you know whether to reboot it or not. Hope this helps someone else.

+2
source

Try Exception Handling :

 import os FILE = 'testing.txt' try: fs = open(FILE, 'a') fs.write('word') os.remove(FILE) fs.write('Nothing....') # Nothing breaks except Exception, e: print "Error:", e print(fs.errors) # No errors 
0
source

There are python bindings for ionization if you need more intelligence than just a try: except: clause. But I think this only applies to Linux (I'm not sure about your platform)

0
source

Another solution I found is to add the "copytruncate" flag to the logrotate configuration. See "Man logrotate" for more information.

0
source

All Articles