Answer by Andrew for How do I check whether a file exists without exceptions?
The easiest way to do this is withimport osif os.path.exists(FILE): # file exists passelse: # file does not exists passfrom the os library, while FILE is the relative path. In Windows this may or many...
View ArticleAnswer by Abhimanyu Sharma for How do I check whether a file exists without...
You Can Use os.path.exists() :import osprint(os.path.exists("file"))Hope It Helps :D
View ArticleAnswer by masterofallthings for How do I check whether a file exists without...
Another possible option is to check whether the filename is in the directory using os.listdir()import osif 'foo.txt' in os.listdir(): # Do thingsthis will return true if it is and false if not
View ArticleAnswer by Memin for How do I check whether a file exists without exceptions?
TL;DRanswer is: pathlib modulePathlib is probably the most modern and convenient way for almost all of the file operations. For the existence of a file or a folder a single line of code is enough.from...
View ArticleAnswer by Sheetal Shahare for How do I check whether a file exists without...
Use the following code:def findfile(filepath): flag = False for filename in os.listdir(filepath) if filename == "your file name": flag = True break else: print("no file found") if flag == True: return...
View ArticleAnswer by Devbrat Shukla for How do I check whether a file exists without...
Use os.path.exists() to check whether file exists or not:def fileAtLocation(filename,path): return os.path.exists(path + filename)filename="dummy.txt"path =...
View ArticleAnswer by Gopinath for How do I check whether a file exists without exceptions?
exists() and is_file() methods of 'Path' object can be used for checking if a given path exists and is a file.Python 3 program to check if a file exists:# File name: check-if-file-exists.pyfrom pathlib...
View ArticleAnswer by Vimal Maheedharan for How do I check whether a file exists without...
import os# for testing purpose args defaulted to current folder & file. # returns True if file founddef file_exists(FOLDER_PATH='../', FILE_NAME=__file__): return os.path.isdir(FOLDER_PATH) \ and...
View ArticleAnswer by Ali Hallaji for How do I check whether a file exists without...
Check file or directory existsYou can follow these three ways:Note1: The os.path.isfile used only for filesimport os.pathos.path.isfile(filename) # True if file existsos.path.isfile(dirname) # False if...
View ArticleAnswer by AbstProcDo for How do I check whether a file exists without...
Date:2017-12-04Every 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...
View ArticleAnswer by durjoy for How do I check whether a file exists without exceptions?
If you imported NumPy already for other purposes then there is no need to import other libraries like pathlib, os, paths, etc.import numpy as npnp.DataSource().exists("path/to/your/file")This will...
View ArticleAnswer by CristiFati for How do I check whether a file exists without...
Although almost every possible way has been listed in (at least one of) the existing answers (e.g. Python 3.4 specific stuff was added), I'll try to group everything together.Note: every piece of...
View ArticleAnswer by Inconnu for How do I check whether a file exists without exceptions?
How do I check whether a file exists, without using the try statement?In 2016, this is still arguably the easiest way to check if both a file exists and if it is a file:import...
View ArticleAnswer by Tom Fuller for How do I check whether a file exists without...
Testing for files and folders with os.path.isfile(), os.path.isdir() and os.path.exists()Assuming that the "path" is a valid path, this table shows what is returned by each function for files and...
View ArticleAnswer by Marcel Wilson for How do I check whether a file exists without...
Adding one more slight variation which isn't exactly reflected in the other answers.This will handle the case of the file_path being None or empty string.def file_exists(file_path): if not file_path:...
View ArticleAnswer by iPhynx for How do I check whether a file exists without exceptions?
You can use os.listdir to check if a file is in a certain directory.import osif 'file.ext' in os.listdir('dirpath'): #code
View ArticleAnswer by Mike McKerns for How do I check whether a file exists without...
I'm the author of a package that's been around for about 10 years, and it has a function that addresses this question directly. Basically, if you are on a non-Windows system, it uses Popen to access...
View ArticleAnswer by KaiBuxe for How do I check whether a file exists without exceptions?
In 2016 the best way is still using os.path.isfile:>>> os.path.isfile('/path/to/some/file.txt')Or in Python 3 you can use pathlib:import pathlibpath = pathlib.Path('/path/to/some/file.txt')if...
View ArticleAnswer by Love and peace - Joe Codeswell for How do I check whether a file...
Here's a 1 line Python command for the Linux command line environment. I find this VERY HANDY since I'm not such a hot Bash guy.python -c "import os.path; print os.path.isfile('/path_to/file.xxx')"I...
View ArticleAnswer by Aaron Hall for How do I check whether a file exists without...
How do I check whether a file exists, using Python, without using a try statement?Now available since Python 3.4, import and instantiate a Path object with the file name, and check the is_file method...
View Article