Check if a file is not open( not used by other process) in Python -
i application, have below requests: 1. there has 1 thread regularly record logs in file. log file rollovered in interval. keeping log files small. 2. there has thread regularly process these log files. ex: move log files other place, parse log's content generate log reports.
but, there has condition second thread can not process log file that's using record log. in code side, pseudocode similars below:
#code in second thread process log files logfile in os.listdir(logfolder): if not file_is_open(logfile) or file_is_use(logfile): processlogfile(logfile) # move log file other place, , generate log report....
so, how check file open or used other process? did research in internet. , have results:
try: myfile = open(filename, "r+") # or "a+", whatever need except ioerror: print "could not open file! please close excel!"
i tried code, doesn't work, no matter use "r+" or "a+" flag
try: os.remove(filename) # try remove directly except oserror e: if e.errno == errno.enoent: # file doesn't exist break
this code can work, can not reach request, since don't want delete file check if open.
an issue trying find out if file being used process possibility of race condition. check file, decide not in use, before open process (or thread) leaps in , grabs (or deletes it).
ok, let's decide live possibility , hope not occur. check files in use other processes operating system dependant.
on linux easy, iterate through pids in /proc. here generator iterates on files in use specific pid:
def iterate_fds(pid): dir = '/proc/'+str(pid)+'/fd' if not os.access(dir,os.r_ok|os.x_ok): return fds in os.listdir(dir): fd in fds: full_name = os.path.join(dir, fd) try: file = os.readlink(full_name) if file == '/dev/null' or \ re.match(r'pipe:\[\d+\]',file) or \ re.match(r'socket:\[\d+\]',file): file = none except oserror err: if err.errno == 2: file = none else: raise(err) yield (fd,file)
on windows not quite straightforward, apis not published. there sysinternals tool (handle.exe
) can used, recommend pypi module psutil
, portable (i.e., runs on linux well, , on other os):
import psutil proc in psutil.process_iter(): try: flist = proc.get_open_files() if flist: print(proc.pid,proc.name) nt in flist: print("\t",nt.path) # catches race condition process ends # before can examine files except psutil.nosuchprocess err: print("****",err)
Comments
Post a Comment