Get file attributes (hidden, readonly, system, archive) in Python

Just started learning Python. How can I get the status of file attributes in Python? I know I can os.chmod(fullname, stat.S_IWRITE)

remove the readonly attribute, but how can I get the status without changing it? I need to get all of the attributes "hidden"

, "system"

, "readonly"

,"archive"

+3


source to share


2 answers


you need to take a look at the module stat

andos.stat



 os.stat(path)

Perform the equivalent of a stat() system call on the given path. (This function follows symlinks; to stat a symlink use lstat().)

The return value is an object whose attributes correspond to the members of the stat structure, namely:

    st_mode - protection bits,
    st_ino - inode number,
    st_dev - device,
    st_nlink - number of hard links,
    st_uid - user id of owner,
    st_gid - group id of owner,
    st_size - size of file, in bytes,
    st_atime - time of most recent access,
    st_mtime - time of most recent content modification,
    st_ctime - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)

      

+2


source


You can use windows API directly



import win32con
import win32api
attrs = win32api.GetFileAttributes(filepath)
attrs & win32con.FILE_ATTRIBUTE_SYSTEM
attrs & win32con.FILE_ATTRIBUTE_HIDDEN

      

+2


source







All Articles