-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnocomment
executable file
·27 lines (22 loc) · 949 Bytes
/
nocomment
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
#!/usr/bin/env python3
import sys
import os
def print_significant_lines(file_path):
try:
with open(file_path, 'r') as file:
for line in file:
# Strip whitespace from the beginning and end of the line
stripped_line = line.strip()
# Check if the line is not empty, not whitespace, and not a comment
if stripped_line and not stripped_line.startswith('#'):
print(line.rstrip()) # Print the line without trailing newline
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.", file=sys.stderr)
except IOError:
print(f"Error: Unable to read file '{file_path}'.", file=sys.stderr)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: nocomment <file_path>", file=sys.stderr)
sys.exit(1)
file_path = sys.argv[1]
print_significant_lines(file_path)