Python 3.3+statfunction
stat.filemode()
mode bits
Turns st_mode into an ls -l style string such as '-rw-r--r--' or 'drwxr-xr-x'. The first character is the file type.
Syntax
stat.filemode(mode)
Pass the full st_mode: without the type bits, e.g. filemode(0o755), the first character is '?'. The permissions are rendered as they are, so on Windows you see -rw-rw-rw- or -r--r--r-- whatever chmod you asked for. For comparing permissions in code the string is awkward; use S_IMODE and octal numbers.
Examples
import os, stat
open('script.py', 'w').close()
os.chmod('script.py', 0o644)
print(stat.filemode(os.stat('script.py').st_mode)) # → -rw-r--r-- (на Windows -rw-rw-rw-)
os.mkdir('pkg')
print(stat.filemode(os.stat('pkg').st_mode)[0]) # → d
import stat
print(stat.filemode(0o755)) # → ?rwxr-xr-x — тип файла не передан
See also
Open in the full glossarydocs.python.orgReport an inaccuracy