Checking if a File is a Block Device in Python
On Linux, block special files represent hardware devices like hard drives, SSDs, and other block devices. They’re typically found in /dev and can be identified using Python’s stat module combined with os.stat(). The Standard Approach import os import stat path = “/dev/sda” try: mode = os.stat(path).st_mode if stat.S_ISBLK(mode): print(f”{path} is a block device”) except FileNotFoundError:…
