17.3. Pathlib Check

  • Path.exists() - Check if path exists

  • Path.is_file() - Check if path is a file

  • Path.is_dir() - Check if path is a directory

  • Path.is_symlink() - Check if path is a symbolic link

  • Path.stat() - Get file or directory status information

17.3.1. SetUp

>>> from pathlib import Path

17.3.2. Exists

>>> mypath = Path('/tmp/myfile.txt')
>>>
>>> mypath.exists()
True

17.3.3. Is File

>>> myfile = Path('/tmp/myfile.txt')
>>>
>>> myfile.is_file()
True

17.3.4. Is Dir

>>> mydir = Path('/tmp/mydir')
>>>
>>> mydir.is_dir()
True

17.3.6. Stat

>>> myfile = Path('/tmp/myfile.txt')
>>>
>>> myfile.stat()
os.stat_result(st_mode=33188, st_ino=134068437, st_dev=16777234, st_nlink=1, st_uid=501, st_gid=0, st_size=0, st_atime=1762531525, st_mtime=1762531525, st_ctime=1762531525)