79146777

Date: 2024-11-01 02:32:13
Score: 2
Natty:
Report link

I am fond of the Pathlib library, where it's pretty elegant and straight to the point.

import os.path
# Method 1: os.path.isfile() - specifically for files (not directories)
if os.path.isfile("file.txt"):
    print("File exists")

# Method 2: pathlib (Python 3)
from pathlib import Path
if Path("file.txt").exists():
    print("File exists")

See the difference?

Reasons:
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Owen Valentinus