使用go和python递归删除.ds store文件的方法
Jun 16, 2016 am 08:45 AM
go
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)
#!/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)
}
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)
}
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)