Python version:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
Import OS, sys;
Def walk (path ):
Print "cd directory:" + path
For item in OS. listdir (path ):
Try:
If (item = ". DS_Store "):
Global count
Count = count + 1
Print "find file. Ds_Store"
OS. remove (path + "/" + item)
Else:
If (OS. path. isdir (path + "/" + item )):
Print "" + path + "/" + item + "is directory"
Walk (path + "/" + item)
Else:
Print "" + path + "/" + item + "is file"
Counter t OSError, e:
Print e
If _ name __= = '_ main __':
Count = 0
If (len (sys. argv)> 1 ):
Root_dir = sys. argv [1]
Else:
Root_dir = OS. getcwd ()
Walk (root_dir)
Print "\ ntotal number:" + str (count)
Go language version:
Copy codeThe Code is as follows:
Package main
Import (
"Flag"
"Fmt"
"OS"
"Path/filepath"
)
Func getFilelist (path string) int {
Count: = 0
Err: = filepath. Walk (path, func (path string, f OS. FileInfo, err error) error {
If f = nil {
Return err
}
If f. IsDir (){
Fmt. Printf ("cd directry % s \ n", path)
Return nil
}
If f. Name () = ". DS_Store "{
Count ++
Println ("" + f. Name () + "is deleted ")
OS. Remove (path)
}
Return nil
})
If err! = Nil {
Fmt. Printf ("filepath. Walk () returned % v \ n", err)
}
Return count
}
Func main (){
Flag. Parse ()
Root: = flag. Arg (0)
Count: = 0
If root = ""{
Crurrent_dir, _: = filepath. Abs (".")
Count = getFilelist (crurrent_dir)
} Else {
Count = getFilelist (root)
}
Fmt. Printf ("\ n total number: % d \ n", count)
}