---
title: "Check if a file exists using Python"  
description: "Check if a file exists using Python"  
author: "Samuel Fernandes"  
published: 2015-05-11  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/23208/check-if-a-file-exists-using-python  
category: "python"  
tags: ["file", "python"]  
reading_time: 2 minutes  

---

# Check if a file exists using Python

How do I [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a [file exists](https://www.mindstick.com/forum/33389/how-do-i-check-if-a-file-exists-in-java), using [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python), without using a try statement?

## Replies

### Reply by Aryan Kumar

To [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) if a file exists in Python, you can use the **os.path** module, which provides a range of functions for working with file paths. Here is an example:

```python
import os.path
filename = "example.txt"
if os.path.isfile(filename):
   print("File exists")
else:
   print("File does not exist")
```

In this example, we first import the **os.path** module. Then we define the name of the file we want to check (**example.txt** in this case). We use the **os.path.isfile()** function to check if the file exists. This function returns **True** if the file exists and is a regular file, and **False** otherwise.

If the file exists, we print the message "File exists". If the file does not exist, we print the message "File does not exist".

Note that this example assumes that the file is in the current working directory of the Python script. If the file is in a different directory, you will need to provide the full path to the file instead of just the filename.

### Reply by Anonymous User

You can also use os.path.isfile\
Return True if path is an existing regular [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp). This follows symbolic links, so both islink() and isfile() can be true for the same path.

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

\
\
You have the os.path.[exists](https://www.mindstick.com/forum/158947/how-do-you-use-the-exists-operator-to-check-for-the-existence-of-records-in-a-subquery) function:\

```
import os.pathos.path.exists(file_path)
```

This returns True for both files and directories.\
Use os.path.isfile to test if it's a file specifically


---

Original Source: https://www.mindstick.com/forum/23208/check-if-a-file-exists-using-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
