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

Evgeny Gerbut picture Evgeny Gerbut · Nov 28, 2014 · Viewed 8.6k times · Source

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"

Answer

loopingz picture loopingz · Oct 30, 2015

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