Skip to content

Commit e3c6477

Browse files
committed
pset6 q1 code
1 parent 3762633 commit e3c6477

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

week6/lines/lines.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
3+
4+
def main():
5+
if len(sys.argv) < 2:
6+
sys.exit("Too few command-line arguments")
7+
elif len(sys.argv) == 2:
8+
file_path = sys.argv[1]
9+
else:
10+
sys.exit("Too many command-line arguments")
11+
12+
if file_path[-3:] == ".py":
13+
try:
14+
file = open(file_path).read().splitlines()
15+
except FileNotFoundError:
16+
sys.exit("File does not exist")
17+
else:
18+
print(get_lines(file))
19+
else:
20+
sys.exit("Not a Python file")
21+
22+
23+
def get_lines(file):
24+
count = 0
25+
for i in file:
26+
if i != "" and i.strip()[0] != "#": # strip used to account for "Assume that any line that starts with #, optionally preceded by whitespace, is a comment"
27+
count += 1
28+
return count
29+
30+
31+
if __name__ == "__main__":
32+
main()

0 commit comments

Comments
 (0)