[python-win32] checking file permission on network share

Tim Golden mail at timgolden.me.uk
Fri Sep 2 11:58:12 CEST 2011


On 31/08/2011 13:38, Steffen Frömer wrote:
> i try to check, if a file on network-share is read only. I tried
> different ways.

What you're checking is the readonly attribute (which has been
there since DOS version x.0). If that is what you intended to
check, then I don't think I can offer anything: you're doing
exactly what I've have done myself. The only qualifier is that
read-only on a directory has no real meaning on Windows: the
OS uses it as a marker that the directory is system-special
(eg My Documents). But if you're checking a file then what
you're doing seems valid.

However, if you're trying to determine whether a file is available
for writing by the current user regardless of the setting of the
readonly attribute, then your code won't help. You've got two
options:

* Attempt to get a write lock on the file (without actually
writing anything, obviously) and if that fails with Access Denied
then you're not allowed to write. (It also fails if the read-only
attribute is set).

<code>
import win32file
import win32con
import ntsecuritycon

filename = r"c:\windows\system32\drivers\etc\hosts",
win32file.CreateFile (
    ntsecuritycon.FILE_GENERIC_WRITE, 0, ## exclusive write
    None, win32con.OPEN_ALWAYS, 0, None
)

</code>

* Use the less-than-straightforward AccessCheck API which isn't
exposed by core Python nor by the pywin32 extensions. (Frankly,
I should just contribute a patch to pywin32 since it gets asked
for often enough).

TJG


More information about the python-win32 mailing list