How to check if file is_open and open_status in python

Are there any python features like:

filename = "a.txt" if is_open(filename) and open_status(filename)=='w': print filename," is open for writing" 
+6
source share
3 answers

This is not exactly what you want, because it just checks to see if a given file is capable of writing. But in case this is useful:

 import os filename = "a.txt" if not os.access(filename, os.W_OK): print "Write access not permitted on %s" % filename 

(I don’t know of any platform independent way to do what you ask)

+7
source

Here is is_open solution for Windows using ctypes:

 from ctypes import cdll _sopen = cdll.msvcrt._sopen _close = cdll.msvcrt._close _SH_DENYRW = 0x10 def is_open(filename): if not os.access(filename, os.F_OK): return False # file doesn't exist h = _sopen(filename, 0, _SH_DENYRW, 0) if h == 3: _close(h) return False # file is not opened by anyone else return True # file is already open 
+1
source

I don't think there is an easy way to do what you want, but you could start by overriding open () and adding your own control code. However, why do you want to do this?

0
source

Source: https://habr.com/ru/post/902143/


All Articles