Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Getting a Unix file type with Python OS module

$
0
0

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?

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


Viewing all articles
Browse latest Browse all 9596

Latest Images

Trending Articles