Python stat Module
Example
Check if a path is a regular file using stat constants:
import os
import stat
mode = os.stat('.').st_mode
if stat.S_ISDIR(mode):
print('Current path is a directory')
else:
print('Current path is not a directory')
Try it Yourself »
Definition and Usage
The stat module defines constants and functions for interpreting the results of os.stat() and similar functions.
Use it to check file types, permissions, and other file system attributes in a portable way.
Members
Member | Description |
---|---|
S_IFDIR | Bit mask for directory. |
S_IFREG | Bit mask for regular file. |
S_IRGRP | Read permission for group. |
S_IROTH | Read permission for others. |
S_IRUSR | Read permission for owner. |
S_ISBLK() | Return True if mode is from a block special device. |
S_ISCHR() | Return True if mode is from a character special device. |
S_ISDIR() | Return True if mode is from a directory. |
S_ISFIFO() | Return True if mode is from a FIFO (named pipe). |
S_ISLNK() | Return True if mode is from a symbolic link. |
S_ISREG() | Return True if mode is from a regular file. |
S_ISSOCK() | Return True if mode is from a socket. |
S_IWGRP | Write permission for group. |
S_IWOTH | Write permission for others. |
S_IWUSR | Write permission for owner. |
S_IXGRP | Execute permission for group. |
S_IXOTH | Execute permission for others. |
S_IXUSR | Execute permission for owner. |
filemode() | Convert file mode to string (like 'drwxr-xr-x'). |