Answer by Khaled.K for How do I check whether a file exists without exceptions?
import os.pathdef isReadableFile(file_path, file_name): full_path = file_path +"/"+ file_name try: if not os.path.exists(file_path): print "File path is invalid." return False elif not...
View ArticleAnswer by codelox for How do I check whether a file exists without exceptions?
import os#Your path here e.g. "C:\Program Files\text.txt"#For access purposes: "C:\\Program Files\\text.txt"if os.path.exists("C:\..."): print "File found!"else: print "File not found!"Importing os...
View ArticleAnswer by Pedro Lobito for How do I check whether a file exists without...
if os.path.isfile(path_to_file): try: open(path_to_file) pass except IOError as e: print "Unable to open file"Raising exceptions is considered to be an acceptable, and Pythonic, approach for flow...
View ArticleAnswer by Zizouz212 for How do I check whether a file exists without exceptions?
Although I always recommend using try and except statements, here are a few possibilities for you (my personal favourite is using os.access):Try opening the file:Opening the file will always verify the...
View ArticleAnswer by Pradip Das for How do I check whether a file exists without...
You can use the "OS" library of Python:>>> import os>>> os.path.exists("C:\\Users\\####\\Desktop\\test.txt") True>>> os.path.exists("C:\\Users\\####\\Desktop\\test.tx")False
View ArticleAnswer by Hanson for How do I check whether a file exists without exceptions?
To check if a file exists, from sys import argvfrom os.path import existsscript, filename = argvtarget = open(filename)print "file exists: %r" % exists(filename)
View ArticleAnswer by bergercookie for How do I check whether a file exists without...
If the file is for opening you could use one of the following techniques:with open('somefile', 'xt') as f: #Using the x-flag, Python3.3 and above f.write('Hello\n')if not os.path.exists('somefile'):...
View ArticleAnswer by Zaheer for How do I check whether a file exists without exceptions?
You can use the following open method to check if a file exists + readable:file = open(inputFile, 'r')file.close()
View ArticleAnswer by Chris for How do I check whether a file exists without exceptions?
You can write Brian's suggestion without the try:.from contextlib import suppresswith suppress(IOError), open('filename'): process()suppress is part of Python 3.4. In older releases you can quickly...
View ArticleAnswer by Cody Piersall for How do I check whether a file exists without...
Python 3.4+ has an object-oriented path module: pathlib. Using this new module, you can check whether a file exists like this:import pathlibp = pathlib.Path('path/to/file')if p.is_file(): # or...
View ArticleAnswer by chad for How do I check whether a file exists without exceptions?
It doesn't seem like there's a meaningful functional difference between try/except and isfile(), so you should use which one makes sense.If you want to read a file, if it exists, dotry: f =...
View ArticleAnswer by un33k for How do I check whether a file exists without exceptions?
This is the simplest way to check if a file exists. Just because the file existed when you checked doesn't guarantee that it will be there when you need to open it.import osfname = "foo.txt"if...
View ArticleAnswer by user2154354 for How do I check whether a file exists without...
You should definitely use this one.from os.path import existsif exists("file") == True: print "File exists."elif exists("file") == False: print "File doesn't exist."
View ArticleAnswer by Yugal Jindle for How do I check whether a file exists without...
Use os.path.isfile() with os.access():import osPATH = './file.txt'if os.path.isfile(PATH) and os.access(PATH, os.R_OK): print("File exists and is readable")else: print("Either the file is missing or...
View ArticleAnswer by Jesvin Jose for How do I check whether a file exists without...
import ospath = /path/to/dirroot,dirs,files = os.walk(path).next()if myfile in files: print "yes it exists"This is helpful when checking for several files. Or you want to do a set intersection/...
View ArticleAnswer by philberndt for How do I check whether a file exists without...
You could try this (safer):try: # http://effbot.org/zone/python-with-statement.htm # 'with' is safer to open a file with open('whatever.txt') as fh: # Do something with 'fh'except IOError as e:...
View ArticleAnswer by pkoch for How do I check whether a file exists without exceptions?
Prefer the try statement. It's considered better style and avoids race conditions.Don't take my word for it. There's plenty of support for this theory. Here's a couple:Style: Section "Handling unusual...
View ArticleAnswer by bortzmeyer for How do I check whether a file exists without...
Unlike isfile(), exists() will return True for directories. So depending on if you want only plain files or also directories, you'll use isfile() or exists(). Here is some simple REPL...
View ArticleAnswer by zgoda for How do I check whether a file exists without exceptions?
Additionally, os.access():if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read()Being R_OK, W_OK, and X_OK the flags to test for permissions (doc).
View ArticleAnswer by rslite for How do I check whether a file exists without exceptions?
If the reason you're checking is so you can do something like if file_exists: open_it(), it's safer to use a try around the attempt to open it. Checking and then opening risks the file being deleted or...
View Article