Quantcast
Channel: How do I check whether a file exists without exceptions? - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by AbstProcDo for How do I check whether a file exists without exceptions?

$
0
0

Date:2017-12-04

Every possible solution has been listed in other answers.

An intuitive and arguable way to check if a file exists is the following:

import osos.path.isfile('~/file.md')  # Returns True if exists, else False# additionaly check a diros.path.isdir('~/folder')  # Returns True if the folder exists, else False# check either a dir or a fileos.path.exists('~/file')

I made an exhaustive cheatsheet for your reference:

#os.path methods in exhaustive cheatsheet{'definition': ['dirname','basename','abspath','relpath','commonpath','normpath','realpath'],'operation': ['split', 'splitdrive', 'splitext','join', 'normcase'],'compare': ['samefile', 'sameopenfile', 'samestat'],'condition': ['isdir','isfile','exists','lexists''islink','isabs','ismount',],'expand': ['expanduser','expandvars'],'stat': ['getatime', 'getctime', 'getmtime','getsize']}

Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>