-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.py
44 lines (39 loc) · 1.27 KB
/
counter.py
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
class Counter:
def __init__(self, path: str, countLines: bool = False):
self.path = path
self.count = 0
self.isIgnored = False
self.notAFile = False
self.countLines = countLines
self.loadIgnore()
def openFile(self):
self.file = open(self.path, 'r')
def countChars(self):
try:
if self.countLines:
self.count = len(self.file.readlines())
else:
self.count = len(self.file.read())
except:
self.count = 0
self.notAFile = True
def closeFile(self):
self.file.close()
def loadIgnore(self):
self.ignore = []
try:
with open('.ignorecount', 'r') as file:
for line in file:
self.ignore.append(line.strip())
except FileNotFoundError:
print('No .ignorecount file found. It\'s not a problem, but you can create it if you want to ignore some files.')
# open file, count chars, close file
def analyze(self):
for ignore in self.ignore:
if ignore in self.path:
self.count = 0
self.isIgnored = True
return
self.openFile()
self.countChars()
self.closeFile()