Search code examples
pythonfilepathoperating-systemcomparison

Check if file or path [Python]


I'm trying to find out if its a path or a file.

I tried it with the os lib from python.

path = '/home/User/Desktop/file.txt'
isFile = os.path.isfile(path)
print(isFile) -> False

Why do I get false and not true?


Solution

  • os.path.isfile will return False if the file doesn't exist. You might want to check os.path.exists(path) as well.