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']}