I need to list all files with the containing directory path inside a folder. I tried to use os.walk, which obviously would be the perfect solution.
However, it also lists hidden folders and files. I’d like my application not to list any hidden folders or files. Is there any flag you can use to make it not yield any hidden files?
Cross-platform is not really important to me, it’s ok if it only works for linux (.* pattern)
No, there is no option to
os.walk()that’ll skip those. You’ll need to do so yourself (which is easy enough):Note the
dirs[:] =slice assignment;os.walkrecursively traverses the subdirectories listed indirs. By replacing the elements ofdirswith those that satisfy a criteria (e.g., directories whose names don’t begin with.),os.walk()will not visit directories that fail to meet the criteria.This only works if you keep the
topdownkeyword argument toTrue, from the documentation ofos.walk():