I’d like to loop over all directories in a path in Python. So I tried something like the following:
import os, glob, sys
os.chdir('/')
dir_list = glob.glob('*')
for wd in dir_list if os.path.isdir(wd) is True:
print(wd + " is a directory.")
It seems I have some kind of syntax error. What should I be doing instead of is True? I had thought that os.path.isdir() returns a boolean.
You are mixing generator expressions with a for loop. You can’t do that. Put the
ifstatement on it’s own line:Note that you do not need to test for
is True; that’s exactly what theifstatement does already.