0

I want to ignore a specific file while using os.walk(path) to iterlate the subTree in the directory.

But for some reason, there is some file I am not expect to parse. e.g. the .git file which includes lots of documents. How can I do this?

 file_to_ignore = '.git'
 for dirname, dirnames, filenames in os.walk(path):
     for filename in filenames:
         if filename == file_to_ignore: continue
         else: 
              #do somthing
Zigii Wong
  • 7,766
  • 8
  • 51
  • 79

1 Answers1

0

Use os module to get extension of file by os.path.splitext() method.

Create ignore extension list if we want to add more ignore extension init

E.g.

True condition:

>>> import os
>>> filename = "abc.git"
>>> ignore = ['.git', ]
>>> os.path.splitext(filename)
('abc', '.git')
>>> os.path.splitext(filename)[1] in ignore
True

False condition:

>>> os.path.splitext("abc.txt")[1] in ignore
False
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56