- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
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.
- How to find if a directory exists in Python?
- Check if a directory is not empty in Java
- How to check if a file is a directory or a regular file in Python?
- Python program to check if a string contains any unique character
- How to check the permissions of a directory using Python?
- Program to check if a string contains any special character in Python
- Check if any interval completely overlaps the other in Python
- Java Program to check if a file or directory is readable
- Linux – How to find the files existing in one directory but not in the other directory?
- How to create a directory using Python?
- How to remove a directory using Python?
- How to delete a Python directory effectively?
- How to rename directory using Python?
- How to check if a vector contains a given value in R?
- How can I iterate over files in a given directory in Python?