简体   繁体   中英

python: how to check Windows folder listdir(List Folder Contents) permission?

We need to check whether the current Windows account has listdir/[List Folder Contents][1] permisison to a windows folder. currently we're using os.access(path, os.R_OK) . however, from my understanding, a windows user can have read access to a folder without List folder Conents permission.

my test: create a fodler, config Security , deny List folder / read data permission.

when I access it in windows file explorer, You don't currently have permission to access this folder -- this is what I'm expecting. however, when I test in python:

>>> p = r'c:\tmp\p'
>>> os.stat(p)
 nt.stat_result(st_mode=16895, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_a
time=1463736204L, st_mtime=1463736204L, st_ctime=1463736135L)
>>> os.access(p, os.R_OK)
True
>>> os.listdir(p)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 WindowsError: [Error 5] Access is denied: 'c:\\tmp\\p/*.*'

os.access => True; os.list => Exception

question

I dont think os.access() is a proper funciton to test the List Folder Conents permission, is there any other funciton/lib in Python can check the listdir(List Folder Contents) permission?

understand that It's easier to ask for forgiveness than for permission , but I still want to find a proper way to verify the listdir permission. thanks!

try using: os.listdir(), it would make a ls on the command line of the current directory, however, the ls would be in array format, use it like this:

import os
ls = os.listdir()
print(ls)

did this answer your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM