Search notes:

Python standard library: os.path

os.path contains functions that are related to files and filename manipulation.

dirname() and basename()

import os
print(os.path.basename('https://abc.xyz/foo/bar/baz/index.html')) # index.html
print(os.path.dirname ('https://abc.xyz/foo/bar/baz/index.html')) # https://abc.xyz/foo/bar/baz

Checking if a file or a directory exists

The os.path module has three functions that allow to check if a file and/or a directory exists:
import os.path

for I in __file__, '.', 'xyz':

    print('File                {:31s}  {}'.format(os.path.basename(I), 'exists' if os.path.isfile(I)  else 'does not exist'))
    print('Directory           {:31s}  {}'.format(os.path.basename(I), 'exists' if os.path.isdir(I)   else 'does not exist'))
    print('Directory or file   {:31s}  {}'.format(os.path.basename(I), 'exists' if os.path.exists(I)  else 'does not exist'))
Github repository about-Python, path: /standard-library/os/path/check-if-file-or-dir-exists.py

See also

The built-in function open() opens files for reading or writing.
The os module.
pathlib
standard library

Index