Quantcast
Channel: How do I check whether a file exists without exceptions? - Stack Overflow
Browsing index pages (47 articles)
↧

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

Code:import oscheck_if_path_exists=lambda path:os.path.exists(path)or:import osdef check_if_path_exists(path): return os.path.exists(path)This code checks whether the file or path specified by the...

View Article


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

This is how i found a list of files(in this images) in one folder and searched it in a folder (with subfolders)# This script concatenates javascript files into a unified js to reduce server...

View Article


Answer by Milad Koohi for How do I check whether a file exists without...

All the answers were related to different versions of PythonBut I generally write the code on the version that is minimal.from os.path import existsfrom sys import argvscript,form_file = argvfile =...

View Article

How do I check whether a file exists without exceptions?

How do I check whether a file exists or not, without using the try statement?

View Article

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

import os.pathif os.path.isfile(filepath): print("File exists")

View Article


Answer by benefactual for How do I check whether a file exists without...

import osos.path.exists(path) # Returns whether the path (directory or file) exists or notos.path.isfile(path) # Returns whether the file exists or not

View Article

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

You have the os.path.exists function:import os.pathos.path.exists(file_path)This returns True for both files and directories but you can instead useos.path.isfile(file_path)to test if it's a file...

View Article

Answer 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


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article
Browsing index pages (47 articles)


Latest Images