-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_info.py
More file actions
50 lines (40 loc) · 1.07 KB
/
Copy pathfile_info.py
File metadata and controls
50 lines (40 loc) · 1.07 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
#!/usr/bin/env python3
#!/usr/bin/env python3
import os
import sys
def parse_file(path):
"""
Parses the text file in the given path and returns space, tab & new line
details.
:arg path: Path of the text file to parse
:return: A tuple with count of spacaes, tabs and lines.
"""
fd = open(path)
i = 0
spaces = 0
tabs = 0
for i,line in enumerate(fd):
spaces += line.count(' ')
tabs += line.count('\t')
#Now close the open file
fd.close()
#Return the result as a tuple
return spaces, tabs, i + 1
def main(path):
"""
Function which prints counts of spaces, tabs and lines in a file.
:arg path: Path of the text file to parse
:return: True if the file exits or False.
"""
if os.path.exists(path):
spaces, tabs, lines = parse_file(path)
print("Spaces %d. tabs %d. lines %d" % (spaces, tabs, lines))
return True
else:
return False
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
sys.exit(-1)
sys.exit(0)