.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
"
\n
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\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
)
}
最后,这是第一篇博客,不知道如何使用博客园的代码编辑器,求指教呀