如何使用Python检查文件的权限?

您可以使用os.access(path,mode)来检查文件权限以及读取,写入和执行权限的模式。例如,

>>> import os
>>> os.access('my_file', os.R_OK) # Check for read access
True
>>> os.access('my_file', os.W_OK) # Check for write access
True
>>> os.access('my_file', os.X_OK) # Check for execution access
False
>>> os.access('my_file', os.F_OK) # Check for existance of file
True

您也可以使用os.stat来获取文件或文件描述符的状态。解释非常复杂,因为它使用位掩码来标识权限。您可以在此处阅读有关此模式的信息:https : //docs.python.org/3/library/os.html#os.stat