Eng
0
Q:

check if a file exists

import os

os.path.exists("file.txt") # Or folder
33
import os.path

if os.path.isfile('filename.txt'):
    print ("File exist")
else:
    print ("File not exist")
23
#using pathlib
from pathlib import Path

file_name = Path("file.txt")
if file_name.exists():
    print("exists") 
else:
    print("does not exist") 
3
>>> import os
>>> os.path.isfile("d:\\Package1\\package1\\fibo.py")
True
>>> os.path.isfile("d:/Package1/package1/fibo.py")
True
>>> os.path.isfile("d:\\nonexisting.txt")
1
file_exists ( string $filename ) : bool
0

Tags

Related

New to Communities?

Join the community