
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to check if a given directory contains any other directory in Python?
To check if a directory contains any directory or not, simply check the reverse, ie, if it contains any entry that is not a file using the isfile method.
For example
import os list_dir = os.listdir('.') for f in list_dir: if not os.path.isfile(os.path.join('.', f)): print("Not a file")
You can also use the all built in to check this.
For example
import os list_dir = [os.path.isfile(os.path.join('.', f)) for f in os.listdir('.')] print(all(list_dir))
The all function will return true only if all entries are files in the given directory.
- Related Articles
- How to check if a File Type Exists in a Directory?
- How to find if a directory exists in Python?
- How to check if a file is a directory or a regular file in Python?
- How to check the permissions of a directory using Python?
- Check if a directory is not empty in Java
- Linux – How to find the files existing in one directory but not in the other directory?
- Program to check if a string contains any special character in Python
- Python program to check if a string contains any unique character
- How to create a directory using Python?
- How to remove a directory using Python?
- How to delete a Python directory effectively?
- Java Program to check if a file or directory is readable
- Check if Directory is Mounted in Bash on Linux
- How can I iterate over files in a given directory in Python?
- Golang Program to check if a file is directory or a file

Advertisements