-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.py
More file actions
executable file
·61 lines (50 loc) · 1.41 KB
/
counter.py
File metadata and controls
executable file
·61 lines (50 loc) · 1.41 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
import os, sys
def check():
print('\033[31mchecking')
print("CWD: " + os.getcwd())
print("Script: " + sys.argv[0])
print(".EXE: " + os.path.dirname(sys.executable))
print("Script dir: " + os.path.realpath(os.path.dirname(sys.argv[0])))
pathname, scriptname = os.path.split(sys.argv[0])
print("Relative script dir: "+pathname)
print("Script dir: "+ os.path.abspath(pathname))
print('\033[0m')
def getDirectory():
return os.getcwd()
# tmp=sys.argv[0].split('/')
# tmp = os.getcwd()
# dir=tmp
# for i in range(len(tmp)-1):
# dir=dir+tmp[i]+'/'
# return dir
root = getDirectory()
ext = '.py'
print('counter is running in ', root)
if len(sys.argv) > 2:
root=sys.argv[2]
ext=sys.argv[1]
if len(sys.argv) > 1:
ext=sys.argv[1]
print('given directory', root)
print('given file extension:', ext)
def countLines(filePath):
with open(filePath) as fp:
line = fp.readline()
cnt = 1
while line:
line = fp.readline()
cnt += 1
print(' total lines: \033[1;34;38m{}\033[0m'.format(cnt))
print('')
return cnt
sum=0
for path, subdirs, files in os.walk(root):
for name in files:
filePath=os.path.join(path, name)
fileName=name
if ext in fileName:
print('\033[1;33;33m', fileName, '\033[0m', 'in directory\033[1;33;33m', path, '\033[0m')
print('-->')
sum=sum+countLines(filePath)
print('total number of \033[1;31;33m', ext, '\033[0m code is:\033[0m \033[1;31;33m', sum, '\033[0m')