os.access returns True, but file is not writable

Viewed 328

I have this code:

import os

print(os.access("tst.txt", os.R_OK))
open("tst.txt", "rb")

# File Permissions:
# Owner: Trusted Installer
# Administrators and my user: deny all permissions

os.access returns True but opening the file raises a PermissionError exception.

And if I try this on another file (like cmd.exe, notepad.exe, System32 dir itself, or ANY other file) it always returns True.

If a file is non-existent it return False.

SO: True on any existing file. Even if there is no write access.

I couldn't find anything about this on the Internet.

Does anybody know why this issue occurs?

EDIT: Of course my script is not running as Administrator.

No matter if I test read or write access.

1 Answers

It looks like os.access does not check user permissions on Windows, it only checks for the readonly flag being set or not.

Note that os.access doesn't check file security on Windows. W_OK just checks the read-only flag. Thus using try...except is really the only possibility if you're on Windows and aren't using the Windows security API .

from http://net-informations.com/python/file/exists.htm

In this case, you may have to wrap a try...except around your code

Related