- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountlines.py
More file actions
Latest commit
47 lines (38 loc) · 1.32 KB
/
Copy pathcountlines.py
File metadata and controls
47 lines (38 loc) · 1.32 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
'countlines.py -- count the lines of files end by ".*" in a folder.'
importos
defcountLines(filename):
count=0
f=open(filename, 'r')
foreachlineinf:
count+=1
f.close()
returncount
defgetFilePathsDict(path):
pyDict= {}
filesList=os.listdir(path)
foreachinfilesList:
filepath=os.path.join(path, each)
ifos.path.isfile(filepath) and (each.endswith('.h') oreach.endswith('.cpp') oreach.endswith('.cu')): # change this line to choose the file kinds you want to count
pyDict[filepath] =0
elifos.path.isdir(filepath):
pyDict=dict(pyDict, **getFilePathsDict(filepath)) # recursive when it is a folder
returnpyDict
defgetTotalNumber(pyDict):
returnsum(pyDict.values())
defshowEachFileNumber(pyDict):
print'File Name\t\tLine Number'
print'-'*40
forkeyinsorted(pyDict):
print'%s\t%s'% (os.path.basename(key).ljust(20), pyDict[key])
defmain():
path=raw_input('Please enter the folder path: ').strip()
pyDict=getFilePathsDict(path)
forkeyinpyDict:
pyDict[key] =countLines(key)
sum=getTotalNumber(pyDict)
print'The total line number is %d.'%sum
print'Details:'
showEachFileNumber(pyDict)
if__name__=='__main__':
main()