欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-14 20:30:52
python版本:复制代码 代码如下:#!/usr/bin/env pythonimport os, sys;def walk(path):  print "cd dire...

python版本:

复制代码 代码如下:

#!/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"
    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 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\n total number:%d\n", count)
}