python:如何检查Windows文件夹listdir(列出文件夹内容)权限?

2024-10-01 09:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我们需要检查当前Windows帐户是否对Windows文件夹具有listdir/[List Folder Contents][1]权限。目前我们正在使用os.access(path, os.R_OK)。但是,据我所知,windows用户可以在没有List folder Conents权限的情况下读取文件夹。在

我的测试: 创建一个fodler,配置Security,拒绝List folder / read data权限。在

当我在windows文件资源管理器中访问它时,You don't currently have permission to access this folder——这就是我所期望的。但是,当我在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()是测试List Folder Conents权限的合适函数,Python中还有其他函数/lib可以检查listdir(List Folder Contents)权限吗?在

请理解It's easier to ask for forgiveness than for permission,但我仍然想找到一种正确的方法来验证listdir权限。谢谢!在


Tags: 文件夹权限accessoswindowscontentsokfolder
1条回答
网友
1楼 · 发布于 2024-10-01 09:33:04

尝试使用:操作系统列表目录(),它将在当前目录的命令行上生成一个ls,但是,ls将是数组格式,如下使用:

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

这个回答了你的问题吗?在

相关问题 更多 >