使用go和python递归删除.ds store文件的方法
ca1725,赵本山新闻,宝鸡招生网
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 %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)
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
python如何查看网页代码
用python查看网页代码的方法:1、使用“import”导入requests包import requests2、使用requests包的get()函数通过网页...
[阅读全文]
-
-
python如何保存文本文件
python保存文本文件的方法:使用python内置的open()类可以打开文本文件,向文件里面写入数据可以用write()函数,写完之后,使用close()函...
[阅读全文]
-
python如何编写win程序
python可以编写win程序。win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法。编写好python程序后py2exe模块即可将...
[阅读全文]
-
-
-
-
-
-
网友评论