使用go和python递归删除.dsstore文件的方法

python版本:

代码如下:

#!/usr/bin/env pythonimport 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”    except 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语言版本:

代码如下:

package mainimport (  ”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\n total number:%d\n”, count)}