34 lines
962 B
Python
34 lines
962 B
Python
import os
|
|
import sys
|
|
|
|
|
|
def main():
|
|
try:
|
|
file_name = sys.argv[1]
|
|
except IndexError:
|
|
print("Provide a file name from command line!")
|
|
sys.exit(1)
|
|
if len(sys.argv) > 2:
|
|
print("Too many command-line arguments")
|
|
sys.exit(1)
|
|
if not os.path.isfile(file_name): # Or try opening and catch FileNotFoundError
|
|
print(f"The file `{file_name}` does not exist!")
|
|
sys.exit(1)
|
|
if not file_name.endswith('.py'):
|
|
print(f"The file `{file_name}` is not a python script (does not end with the proper suffix)!")
|
|
sys.exit(1)
|
|
line_count = count_lines(file_name)
|
|
print(line_count)
|
|
|
|
|
|
def count_lines(file_name: str) -> int:
|
|
line_count = 0
|
|
with open(file_name, 'r') as file_object:
|
|
for line in file_object:
|
|
if len(line.strip()) > 0 and not line.strip().startswith('#'):
|
|
line_count += 1
|
|
return line_count
|
|
|
|
if __name__ == "__main__":
|
|
main()
|