Languages
[Edit]
EN

Python - check if file is regular file

0 points
Created by:
Chronal
547

In this article, we would like to show you how to check if the file is a regular file in Python.

Quick solution:

import os.path

is_file = os.path.isfile("example.txt")
print(is_file)  # True

 

Practical example

In this example, we check if the example.txt file is a regular file using isfile() method from os.path module.

import os.path

is_file = os.path.isfile("example.txt")
print(is_file)  # True

if the file isn't directly in our project we need to specify the path:

import os.path

is_file = os.path.isfile("C:\\example_directory\example.txt")
print(is_file)  # True
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - file operations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join