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

有人穷途末路,夕阳西下,却是有人的崭新起点,旭日东升。如果没有你,我不爱旅游,再美的风景也比不过你的发梢。

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 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)
}

本文使用go和python递归删除.ds store文件的方法到此结束。真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自我的声音,走出属于自我的道路。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战