Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore some folders in os.listdir(path) Python

Tags:

python

My python script executes a call to os.listdir(path) where the path contains folders that I need to work on one by one. There are a few folders that need special treatment and need to be out of the list.

How can I exclude those folders from the list returned by os.listdir(path)?

like image 436
Tarandeep Kalra Avatar asked Dec 10 '22 21:12

Tarandeep Kalra


1 Answers

ignored = {"folder_one", "folder_two", "folder_three"}
folders = [x for x in os.listdir(path) if x not in ignored]
like image 174
Delgan Avatar answered Jan 05 '23 00:01

Delgan