Answer the question
In order to leave comments, you need to log in
Python: how to check if there are any files in a directory?
Please tell me how to use Python to check if there are any files in a certain directory without specifying exactly which ones (without specifying names or extensions). Interested in simply the fact of the presence or absence of something. It is desirable so that the script execution does not stop on an error, but gives a log ("files are present" / "absent").
Answer the question
In order to leave comments, you need to log in
import os
if os.listdir(path):
#Есть файлы
else:
#Нет файлов
import os
os.listdir(path) - displays a list of all files in the current folder
len(os.listdir(path)) - if there are files, it should return a value other than 0, because the list is not empty in this case. If there are no files or subdirectories, it will return 0.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question