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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745042927, '13.58.196.90', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/standard-library/os/path/index(70): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78