Use go and python recursion to delete the. DS Store File Method _python

Source: Internet
Author: User

Python version:

Copy Code code as follows:

#!/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 language version:

Copy Code code as follows:

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 \ nthe total number:%d\n", count)
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.