in Technology by

How do I check whether a file exists without exceptions?

1 Answer

0 votes
by

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 moved or something between when you check and when you try to open it.

If you're not planning to open the file immediately, you can use os.path.isfile

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

import os.path
os.path.isfile(fname) 

if you need to be sure it's a file.

Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7):

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
    # file exists

To check a directory, do:

if my_file.is_dir():
    # directory exists

To check whether a Path object exists independently of whether is it a file or directory, use exists():

if my_file.exists():
    # path exists

You can also use resolve(strict=True) in a try block:

try:
    my_abs_path = my_file.resolve(strict=True)
except FileNotFoundError:
    # doesn't exist
else:
    # exists

Related questions

0 votes
    Can we check the existence of a file without using try statement in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    can you please please let me know how can I check whether on which mode (32 or 64 bit) my java ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I've got a table of URLs and I don't want any duplicate URLs. How do I check to see if ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    I want to include a batch file rename functionality in my application. A user can type a destination ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    You can check to see whether an R object is NULL with the _________ function. (a) is.null() (b) is. ... and Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Write a script in python to check whether your age is “major” or “minor” Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    WAP in java to check whether the sum of second and third digit of a 4 digit no. , is even or odd? Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    Mathematical function to check whether a number is positive or negative in c# Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    write a program to check whether the entered number is positive or negative or zero Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    write a program to input a number. check and display whether it is divisible by 100 Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    Help needed Please write a program to check whether a number is special number or not . 100 points Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    String Slicing means: 1.to convert the string in capital 2.to check whether a string has more than one ... in reverse order computet Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
...