Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unix file type with Python os module

I would like to get the unix file type of a file specified by path (find out whether it is a regular file, a named pipe, a block device, ...)

I found in the docs os.stat(path).st_type but in Python 3.6, this seems not to work.

Another approach is to use os.DirEntry objects (e. g. by os.listdir(path)), but there are only methods is_dir(), is_file() and is_symlink().

Any ideas how to do it?

like image 244
karlosss Avatar asked Jun 16 '17 18:06

karlosss


People also ask

How do I see a file type in Python?

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

How do I open an os module file in Python?

open() method in Python is used to open a specified file path and set various flags according to the specified flags and its mode according to specified mode. This method returns a file descriptor for newly open file. The returned file descriptor is non-inheritable.

How do I identify a file type?

Right-click the file. Select the Properties option. In the Properties window, similar to what is shown below, see the Type of file entry, which is the file type and extension.

How do I get the file path in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.


2 Answers

You use the stat module to interpret the result of os.stat(path).st_mode.

>>> import os
>>> import stat
>>> stat.S_ISDIR(os.stat('/dev/null').st_mode)
False
>>> stat.S_ISCHR(os.stat('/dev/null').st_mode)
True

You can make a general function to return the determined type. This works for both Python 2 and 3.

import enum
import os
import stat

class PathType(enum.Enum):
    dir = 0  # directory
    chr = 1  # character special device file
    blk = 2  # block special device file
    reg = 3  # regular file
    fifo = 4  # FIFO (named pipe)
    lnk = 5  # symbolic link
    sock = 6  # socket
    door = 7  # door  (Py 3.4+)
    port = 8  # event port  (Py 3.4+)
    wht = 9  # whiteout (Py 3.4+)

    unknown = 10

    @classmethod
    def get(cls, path):
        if not isinstance(path, int):
            path = os.stat(path).st_mode
        for path_type in cls:
            method = getattr(stat, 'S_IS' + path_type.name.upper())
            if method and method(path):
                return path_type
        return cls.unknown

PathType.__new__ = (lambda cls, path: cls.get(path))
>>> PathType('/dev/null')
<PathType.chr: 1>
>>> PathType('/home')
<PathType.dir: 0>
like image 152
Artyer Avatar answered Oct 04 '22 03:10

Artyer


Python 3.6 has pathlib and its Path objects have methods:

  • is_dir()
  • is_file()
  • is_symlink()
  • is_socket()
  • is_fifo()
  • is_block_device()
  • is_char_device()

pathlib takes a bit to get used to (at least for me having come to Python from C/C++ on Unix), but it is a nice library

like image 37
Anthon Avatar answered Oct 04 '22 02:10

Anthon