您好,如何忽略像.snaptshot这样的目录

时间:2018-05-04 10:14:42

标签: python os.walk

在Python中使用.snapshot时,如何忽略.gitos.walk这样的目录?

你能帮助我吗?

这是我的代码:

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()

1 个答案:

答案 0 :(得分:0)

如何查看&#39; .snapshot&#39;或者&#39; .git&#39;在一个路径和continue如果找到?

...
for path, dirs, files in os.walk(datasource):
    if '.snapshot' in path or '.git' in path:
        continue
...