Skip to content

Commit 6b73623

Browse files
authored
PR #20: Remove-Empty-Lines-Program
Remove empty lines Merge pull request #20 from adedayoprcs/remove-empty-lines
2 parents fcddbca + b26bc26 commit 6b73623

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Remove-Empty-Lines/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# Remove Empty Lines from Text File
3+
4+
This Python script removes empty lines from a text file using command-line arguments.
5+
6+
## Usage
7+
8+
1. Run the script with:
9+
10+
```
11+
python remove_empty_lines.py input_file output_file
12+
```
13+
14+
Replace `input_file` with the input file path and `output_file` with the output file path.
15+
16+
2. The script processes `input_file`, removes empty lines, and saves the result in `output_file`.
17+
18+
## Example
19+
20+
```shell
21+
python remove_empty_lines.py input.txt output.txt
22+
```
23+
24+
This command processes `input.txt` and saves the cleaned content to `output.txt`.
25+
26+
## Error Handling
27+
28+
- Handles incorrect usage with a usage message and exit.
29+
- Gracefully handles file not found errors and other exceptions, providing informative messages.
30+
31+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
if len(sys.argv) != 3:
3+
print("Usage: python remove_empty_lines.py input_file output_file")
4+
sys.exit(1)
5+
input_file = sys.argv[1]
6+
output_file = sys.argv[2]
7+
try:
8+
with open(input_file, "r") as in_file, open(output_file, "w") as out_file:
9+
for line in in_file:
10+
if line.strip():
11+
out_file.write(line)
12+
print(f"Empty lines removed and saved to {output_file}")
13+
except FileNotFoundError:
14+
print("File not found. Please check the file paths.")
15+
except Exception as e:
16+
print(f"An error occurred: {str(e)}")

0 commit comments

Comments
 (0)