usr bin env python import sys os def usage print __file__ dirname def

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#! /usr/bin/env python
import sys, os
def usage():
print __file__ + ' <dirname>'
def cleandir(dirname, ends):
files = os.listdir(dirname)
for f in files:
filename = os.path.join(dirname, f)
if os.path.isdir(filename):
cleandir(filename, ends)
else:
for end in ends:
if filename.endswith(end):
os.unlink(filename)
def main():
try:
dirname = sys.argv[1]
except IndexError:
usage()
sys.exit(-1)
ends = ('.pyc', '.pyo', '.pyd', '~', '.swp')
cleandir(dirname, ends)
if __name__ == '__main__':
main()