-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlines.py
39 lines (31 loc) · 963 Bytes
/
lines.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
import sys
import os
def main():
if len(sys.argv) < 2:
print("Too few command-line arguments")
sys.exit(1)
elif len(sys.argv) > 2:
print("Too many command-line arguments")
sys.exit(1)
elif not os.path.isfile(sys.argv[1]):
print("File does not exist")
sys.exit(1)
elif not sys.argv[1].endswith(".py"):
print("Not a Python file")
sys.exit(1)
else:
print(counter(sys.argv[1]))
def counter(python_file):
ignored_lines = 0
with open(python_file, "r") as lines:
total_lines = list(enumerate(lines.readlines(), start=1))
for line_number, lines in total_lines:
if (
lines.strip().startswith("#")
or lines.strip().startswith("\n")
or lines.isspace()
):
ignored_lines += 1
return int(total_lines[-1][0]) - ignored_lines
if __name__ == "__main__":
main()