---
description: Master the knack of verifying file existence in Python with our clear, concise guide on Python Check if File Exists. Various methods are explained for your ease.
title: Python Check if File Exists (with Examples)
image: https://www.guru99.com/images/python-check-if-file-exists.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Python lets you check whether a file or directory exists using the os.path module and pathlib. Here you will learn os.path.exists(), os.path.isfile(), os.path.isdir(), and pathlib.Path.exists(), each with a clear code example.

- ✅ **exists():** os.path.exists(path) returns True if a file or directory exists.
- 📄 **isfile() / isdir():** These return True only for an existing file or directory respectively.
- 🐍 **pathlib:** pathlib.Path(path).exists() works in Python 3.4 and later.

Read More

![Python Check if File Exists](https://www.guru99.com/images/python-check-if-file-exists.png)

## Python exists()

**Python exists()** method is used to check whether specific file or directory exists or not. It is also used to check if a path refers to any open file descriptor or not. It returns boolean value true if file exists and returns false otherwise. It is used with os module and os.path sub module as os.path.exists(path).

In this Python file exists tutorial, we will learn how to determine whether a file (or directory) exists using Python. To check if file exists Python, we use Built-in library Python check if file exists functions.

There are different ways to verify a file or Python check if directory exists, using functions as listed below.

## How to Check If a File Exists in Python using os.path.exists()

Using path.exists you can quickly check that a file or directory exists. Here are the steps for Python check file exists or not:

**Steps 1) Import the os.path module**

Before you run the code, it is important that you import the os.path module.

```
import os.path
from os import path
```

**Steps 2) Use path.exists() funtion**

Now, use the path.exists() function to Python check if a file exists.

```
path.exists("guru99.txt")
```

**Steps 3) Run the code given below**

Here is the complete code

```
import os.path
from os import path

def main():

   print ("File exists:"+str(path.exists('guru99.txt')))
   print ("File exists:" + str(path.exists('career.guru99.txt')))
   print ("directory exists:" + str(path.exists('myDirectory')))

if __name__== "__main__":
   main()
```

In our case only file guru99.txt is created in the working directory

**Output:**

```
File exists: True
File exists: False
directory exists: False

```

**Don't Miss:**

- [Facebook Login using Python: FB Login Example](https://www.guru99.com/facebook-login-using-python.html)
- [Python vs JavaScript: Key Difference Between Them](https://www.guru99.com/python-vs-javascript.html)
- [Python New Line: How to Print WITHOUT Newline in Python](https://www.guru99.com/print-without-newline-python.html)
- [Python List index() with Example](https://www.guru99.com/python-list-index.html)

## Python isfile()

The **Python isfile()** method is used to find whether a given path is an existing regular file or not. It returns a boolean value true if the specific path is an existing file or else it returns false. It can be used by the syntax : os.path.isfile(path).

## os.path.isfile()

We can use the isfile command to check whether a given input is a file or not.

```
import os.path
from os import path

def main():

	print ("Is it File?" + str(path.isfile('guru99.txt')))
	print ("Is it File?" + str(path.isfile('myDirectory')))
if __name__== "__main__":
	main()
```

**Output:**

```
Is it File? True
Is it File? False

```

## os.path.isdir()

If we want to confirm that a given path points to a directory, we can use the os.path.dir() function

```
import os.path
from os import path

def main():

   print ("Is it Directory?" + str(path.isdir('guru99.txt')))
   print ("Is it Directory?" + str(path.isdir('myDirectory')))

if __name__== "__main__":
   main()
```

**Output:**

```
Is it Directory? False
Is it Directory? True

```

## pathlibPath.exists() For Python 3.4

[Python](https://www.guru99.com/python-tutorials.html) 3.4 and above versions have pathlib Module for handling with file system path. It uses object-oriented approach to Python check if folder exists or not.

```
import pathlib
file = pathlib.Path("guru99.txt")
if file.exists ():
    print ("File exist")
else:
    print ("File not exist")

```

**Output:**

```
File exist

```

**Complete Code**

Here is the complete code

```
import os
from os import path

def main():
    # Print the name of the OS
    print(os.name)
#Check for item existence and type
print("Item exists:" + str(path.exists("guru99.txt")))
print("Item is a file: " + str(path.isfile("guru99.txt")))
print("Item is a directory: " + str(path.isdir("guru99.txt")))

if __name__ == "__main__":
    main()

```

**Output:**

```
Item exists: True
Item is a file: True
Item is a directory: False

```

## FAQs

✅ How do you check if a file exists in Python?

Use os.path.exists(‘file.txt’). It returns True if the file or directory exists, otherwise False.

📄 What does os.path.isfile() do?

os.path.isfile(path) returns True only if the path is an existing regular file.

📁 How do you check if a directory exists?

Use os.path.isdir(path). It returns True only when the path is an existing directory.

🐍 How do you check file existence with pathlib?

Create pathlib.Path(‘file.txt’) and call exists(). It returns True if the file exists (Python 3.4+).

🆚 What is the difference between exists() and isfile()?

exists() is True for files and directories, while isfile() is True only for files.

🤖 How can AI tools like GitHub Copilot help check file existence?

AI assistants like GitHub Copilot autocomplete os.path.exists() and pathlib checks and add existence guards before opening files.

🧠 Can AI automate adding file-existence checks?

Yes. AI-powered tools insert os.path.exists() guards and refactor code to check paths before reading or writing.

❓ Does os.path.exists() work for directories too?

Yes. os.path.exists() returns True for both files and directories; use isfile() or isdir() to distinguish them.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-check-if-file-exists.png","url":"https://www.guru99.com/images/python-check-if-file-exists.png","width":"700","height":"250","caption":"Python Check if File Exists","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-check-if-file-exists.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-check-if-file-exists.html","name":"Python Check if File Exists (with Examples)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-check-if-file-exists.html#webpage","url":"https://www.guru99.com/python-check-if-file-exists.html","name":"Python Check if File Exists (with Examples)","dateModified":"2026-10-07T13:58:10+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-check-if-file-exists.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-check-if-file-exists.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python Check if File Exists (with Examples)","description":"Master the knack of verifying file existence in Python with our clear, concise guide on Python Check if File Exists. Various methods are explained for your ease.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T13:58:10+05:30","image":{"@id":"https://www.guru99.com/images/python-check-if-file-exists.png"},"copyrightYear":"2026","name":"Python Check if File Exists (with Examples)","subjectOf":[{"@type":"HowTo","name":"How to Check If a File Exists in Python","description":"Here is a step by step process on how to check if a file exists in python","step":[{"@type":"HowToStep","name":"Step 1) Import the os.path module","text":"Before you run the code, it is important that you import the os.path module.","url":"https://www.guru99.com/python-check-if-file-exists.html#step1"},{"@type":"HowToStep","name":"Step 2) Use path.exists() funtion","text":"Now, use the path.exists() function to Python check if a file exists.","url":"https://www.guru99.com/python-check-if-file-exists.html#step2"},{"@type":"HowToStep","name":"Step 3) Run the code given below","text":"Here is the complete code, check output.","url":"https://www.guru99.com/python-check-if-file-exists.html#step3"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do you check if a file exists in Python?","acceptedAnswer":{"@type":"Answer","text":"Use os.path.exists('file.txt'). It returns True if the file or directory exists, otherwise False."}},{"@type":"Question","name":"What does os.path.isfile() do?","acceptedAnswer":{"@type":"Answer","text":"os.path.isfile(path) returns True only if the path is an existing regular file."}},{"@type":"Question","name":"How do you check if a directory exists?","acceptedAnswer":{"@type":"Answer","text":"Use os.path.isdir(path). It returns True only when the path is an existing directory."}},{"@type":"Question","name":"How do you check file existence with pathlib?","acceptedAnswer":{"@type":"Answer","text":"Create pathlib.Path('file.txt') and call exists(). It returns True if the file exists (Python 3.4+)."}},{"@type":"Question","name":"What is the difference between exists() and isfile()?","acceptedAnswer":{"@type":"Answer","text":"exists() is True for files and directories, while isfile() is True only for files."}},{"@type":"Question","name":"How can AI tools like GitHub Copilot help check file existence?","acceptedAnswer":{"@type":"Answer","text":"AI assistants like GitHub Copilot autocomplete os.path.exists() and pathlib checks and add existence guards before opening files."}},{"@type":"Question","name":"Can AI automate adding file-existence checks?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered tools insert os.path.exists() guards and refactor code to check paths before reading or writing."}},{"@type":"Question","name":"Does os.path.exists() work for directories too?","acceptedAnswer":{"@type":"Answer","text":"Yes. os.path.exists() returns True for both files and directories; use isfile() or isdir() to distinguish them."}}]}],"@id":"https://www.guru99.com/python-check-if-file-exists.html#schema-150128","isPartOf":{"@id":"https://www.guru99.com/python-check-if-file-exists.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-check-if-file-exists.html#webpage"}}]}
```
