Add nocomment

This commit is contained in:
Sangye Ince-Johannsen 2024-10-08 06:40:59 +02:00
parent 9a891b2b01
commit e3a51e1299

27
nocomment Normal file
View file

@ -0,0 +1,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)