加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 20:03:32 所属栏目:Python 来源:网络整理
导读: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

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 %sn",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 %vn",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("nn total number:%dn",count)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读