Just started learning Python. How can i get a status of file's attributes in Python? I know that os.chmod(fullname, stat.S_IWRITE)
delete readonly attribute, but how can i get status without changing it? I need to get all of the attributes of "hidden"
, "system"
, "readonly"
, "archive"
Asked
Active
Viewed 1.3k times
9

Mazdak
- 105,000
- 18
- 159
- 188

Evgeny Gerbut
- 390
- 1
- 4
- 10
-
which operating system, on linux for instance a hidden file is any file with a `.` as the first character of the name, on windows though I think it is a file attribute. – Tony Suffolk 66 Nov 29 '14 at 09:58
3 Answers
9
You can use directly the Windows API like that
import win32con
import win32api
attrs = win32api.GetFileAttributes(filepath)
attrs & win32con.FILE_ATTRIBUTE_SYSTEM
attrs & win32con.FILE_ATTRIBUTE_HIDDEN

loopingz
- 1,149
- 17
- 19
-
-
The FileAttributes is flags integer so you need to check each bit, each bit represent a boolean value. – loopingz Mar 11 '19 at 13:46
-
Well after getting attr, how to extract those hidden or archived and other values from this? – Ratul Hasan Dec 02 '21 at 15:02
5
you need to take a look at module stat
and os.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)

Hackaholic
- 19,069
- 5
- 54
- 72
3
If you are using python 3.4+ you can use pathlib stat method.
from pathlib import Path
print(Path(r"D:\temp\test.txt").stat())
Output:
os.stat_result(
st_mode=33206,
st_ino=204632308068721491,
st_dev=67555953,
st_nlink=1,
st_uid=0,
st_gid=0,
st_size=4,
st_atime=1550757968,
st_mtime=1550757968,
st_ctime=1550757951
)

Vlad Bezden
- 83,883
- 25
- 248
- 179