Skip to content

Instantly share code, notes, and snippets.

@TheMatt2
Created October 9, 2023 01:48
Show Gist options
  • Save TheMatt2/4c2ff78485ec82f9b1308e5dd07186b1 to your computer and use it in GitHub Desktop.
Save TheMatt2/4c2ff78485ec82f9b1308e5dd07186b1 to your computer and use it in GitHub Desktop.

Python File Type Support

Python os.stat() file types

https://docs.python.org/3/library/stat.html

Python stat module supported "file" types

  • "file" is the UNIX sense that everything is a file
Function Type
stat.S_ISDIR(mode) directory
stat.S_ISCHR(mode) character special device file
stat.S_ISBLK(mode) block special device file
stat.S_ISREG(mode) regular file
stat.S_ISFIFO(mode) FIFO (named pipe)
stat.S_ISLNK(mode) symbolic link
stat.S_ISSOCK(mode) socket
stat.S_ISDOOR(mode) door (new Python 3.4)
stat.S_ISPORT(mode) event port (new Python 3.4)
stat.S_ISWHT(mode) whiteout (new Python 3.4)

What is a door anyway?

What is an event port anyway?

What is a whiteout anyway?

  • https://lwn.net/Articles/265240/
  • https://lwn.net/Articles/325369/
  • Summarize:
    • A whiteout is a thing that exists on some "overlay" filesystem.
    • It represents "there is no file here", when there maybe a file existing at a lower level of the overlay.
    • There is question of how good of an idea they are, and support is limited.
    • Base linux does not support it, but BSD does.

Python os.path file types

https://docs.python.org/3/library/os.path.html

Function Type
os.path.isfile(path) regular file
os.path.isdir(path) directory
os.path.isjunction(path) junction (New in version 3.12)
os.path.islink(path) symbolic link
os.path.ismount(path) mount point
os.path.isdevdrive(path) Windows Dev Drive (New in version 3.12)

What is a junction?

What is a Windows Dev Drive?

  • https://docs.python.org/3/library/os.path.html#module-os.path
  • A Dev Drive is optimized for developer scenarios, and offers faster performance for reading and writing files.
  • It is recommended for use for source code, temporary build directories, package caches, and other IO-intensive operations.
  • Very strange.

Python pathlib file types

Function Type
Path.is_dir() directory
Path.is_file() regular file
Path.is_junction() junction (New in version 3.12)
Path.is_mount() mount point (New in version 3.7; 3.12: Windows support was added)
Path.is_symlink() symbolic link
Path.is_socket() Unix socket
Path.is_fifo() FIFO
Path.is_block_device() block device
Path.is_char_device() character device

Python file type detection

File Type os.stat() os.path pathlib
regular file
directory
symbolic link
mount point
socket
FIFO
block device
character device
junction
door
event port
whiteout
Windows Dev Drive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment