8

Is there any python functions such as:

filename = "a.txt"
if is_open(filename) and open_status(filename)=='w':
   print filename," is open for writing"
Serenity
  • 35,289
  • 20
  • 120
  • 115
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117

5 Answers5

6

This is not quite what you want, since it just tests whether a given file is write-able. But in case it's helpful:

import os

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

(I'm not aware of any platform-independent way to do what you ask)

Ben Hoffstein
  • 102,129
  • 8
  • 104
  • 120
  • 1
    Thanks! But it seems that os.access only checks the readability/writability of a file, but I want to check whether the file has ALREADY been opened or not. Thanks for further suggestions. – Hailiang Zhang Nov 22 '11 at 18:46
  • You may be technically correct, but it sure isn't helpful to have this as the first result when searching for how to test filehandle status. – SkyLeach May 03 '18 at 13:36
3

Here is an 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
Toni Ruža
  • 7,462
  • 2
  • 28
  • 31
1

I think the best way to do that is to try.

def is_already_opened_in_write_mode(filename)
     if os.path.exists(filename):
         try:
             f = open(filename, 'a')
             f.close()
         except IOError:
             return True
     return False
PilouPili
  • 2,601
  • 2
  • 17
  • 31
  • ⚠ DANGER − Calling `open(filename, 'w')` will delete the contents of the file. A *slightly* better solution would be to call `open(filename, 'a')`. Better yet, don't open the file at all, and use the API function for checking if a file is writable. – Alex Quinn Oct 22 '19 at 01:17
  • If I recall correctly thé question was to detect if à file was opened by another program not if it is writable (hence just cheking access) – PilouPili Oct 22 '19 at 09:25
0

There is a attribute to check the open status or mode of a file.

filename = open('open.txt','r')
print(filename.mode)

Python has an inbuilt attribute called 'mode'. Above code will return 'r'. Similarly if you open a file in 'w' mode above code will return 'w'

You can also check if a file is open or closed

print(filename.closed)

This will return 'FALSE' as the file is in open state.

filename.closed
print(filename.closed)

This will return 'TRUE' because the file is now in close state.

Thank you

-1

I don't think there is an easy way to do what you want, but a start could be to redefine open() and add your own managing code. That said, why do you want to do that?

daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
  • This would only help, if the responsible code were also in Python and under control of the developer. – guidot Mar 30 '20 at 15:52