0

How does one ignore a directory like .snapshot or .git when using os.walk in Python?

Can you help me?

Here is my code:

datasource = "/home/me/PYTHON/OGL_TOOLS"

def scanfolder():
    for path, dirs, files in os.walk(datasource):
        depth = path.count('/') - 6
        maxdepth = 2

        if re.match ('^\S+.snapshot$', path) in dirs:
                print ('bad format')
                dirs.remove('.snapshot')

        elif depth <= maxdepth:
                for f in files:
                        #print (f)
                        if f.endswith('.txt'):
                                print (os.path.join(path, f))
                                filelog.write('\t{} =>  bonne syntaxe\n'.format(os.path.join(path, f)))


scanfolder()
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    Possible duplicate of [os.walk without hidden folders](https://stackoverflow.com/questions/13454164/os-walk-without-hidden-folders) – khelwood May 04 '18 at 10:21

1 Answers1

0

What about checking for '.snapshot' or '.git' in a path and continue if found?

...
for path, dirs, files in os.walk(datasource):
    if '.snapshot' in path or '.git' in path:
        continue
...
alec_djinn
  • 10,104
  • 8
  • 46
  • 71