-
Notifications
You must be signed in to change notification settings - Fork 3
/
lines.py
32 lines (25 loc) · 850 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
import sys
def main():
if len(sys.argv) < 2:
sys.exit("Too few command-line arguments")
elif len(sys.argv) == 2:
file_path = sys.argv[1]
else:
sys.exit("Too many command-line arguments")
if file_path[-3:] == ".py":
try:
file = open(file_path).read().splitlines()
except FileNotFoundError:
sys.exit("File does not exist")
else:
print(get_lines(file))
else:
sys.exit("Not a Python file")
def get_lines(file):
count = 0
for i in file:
if i.strip() != "" and i.strip()[0] != "#": # strip used to account for "Assume that any line that starts with #, optionally preceded by whitespace, is a comment" and if a line has more than one whitespace
count += 1
return count
if __name__ == "__main__":
main()