Last active
August 29, 2015 13:57
-
-
Save ShawnMilo/9727816 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Delete *.pyc files. | |
// Basically does this: | |
// find . -name '*.pyc' -exec rm {} \; | |
import ( | |
"log" // for writing errors and quitting if something breaks | |
"os" // for command-line args and getting file info | |
"path/filepath" // for walking a directory tree | |
"strings" // for checking filename substrings | |
) | |
// main reads the command-line args and kicks off | |
// the walk through the directory tree, using the custom WalkFunc. | |
func main() { | |
var dir string | |
var err error | |
if len(os.Args) > 1 { | |
dir = os.Args[1] | |
} else { | |
dir, err = os.Getwd() | |
if err != nil { | |
log.Fatal("Couldn't get current directory.") | |
} | |
} | |
// Sanity check. | |
if dir == "" { | |
log.Fatal("Couldn't determine working directory.") | |
} | |
stat, err := os.Stat(dir) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if stat.IsDir() == false { | |
log.Fatal(dir, " is not a directory.") | |
} | |
filepath.Walk(dir, hunt) | |
} | |
// hunt is a WalkFunc, as defined in path/filepath. | |
func hunt(path string, info os.FileInfo, err error) error { | |
if strings.HasSuffix(path, ".pyc") { | |
err := os.Remove(path) | |
// Make noise, but don't kill the whole program. | |
if err != nil { | |
log.Println(err) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated recently to remove concurrency, since it was unnecessary and sometimes led to the program exiting before all files were deleted.