zoukankan      html  css  js  c++  java
  • mac os去除去除.DS_Store文件--使用python和go(原创)

    .DS_Store (英文全称 Desktop Services Store)是一种由苹果公司的Mac OS X操作系统所创造的隐藏文件,目的在于存贮文件夹的自定义属性,例如文件们的图标位置或者是背景色的选择。——见维基百科

    有的时候,.DS_Store让人觉得特别麻烦,比如压缩文件或者采用版本控制软件如svn和git的时候,因此写了一段代码递归删除.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 " total 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 ", 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 ", 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(" total number:%d ", count)
    }
     
    最后,这是第一篇博客,不知道如何使用博客园的代码编辑器,求指教呀
  • 相关阅读:
    cin.clear()与cin.sync()的使用
    win10无法连接windows服务器,无法连接SENS服务
    Error:java: 错误: 不支持发行版本 5
    IDEA 整合 SSM 框架学习
    python requests请求状态码异常处理
    python+requests接口自动化入门--返回值的处理
    Python bug打断点调试学习
    locust知识导航栏
    ERROR: unknown command "pip"
    Python3 集合
  • 原文地址:https://www.cnblogs.com/dyf6372/p/3527261.html
Copyright © 2011-2022 走看看