We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3762633 commit e3c6477Copy full SHA for e3c6477
week6/lines/lines.py
@@ -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
18
+ print(get_lines(file))
19
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