使用go和python递归删除.ds store文件的方法
程序员文章站
2022-04-19 09:53:44
...
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核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
上一篇: django进阶学习记录
下一篇: 详解Python中的字符串与字符编码介绍
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论