Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: remove_blank_lines.py
- # Author: Jeoi Reqi
- # This script removes blank lines from a text file, preserving unique lines in sorted order & saves to file.
- import os
- # Function to remove blank lines from text
- def remove_blank_lines(input_file, output_file):
- with open(input_file, 'r', encoding='utf-8') as file:
- lines = file.readlines()
- non_blank_lines = [line for line in lines if line.strip() != '']
- # Write Data To File
- with open(output_file, 'w', encoding='utf-8') as output: # Use utf-8 Encoding
- output.writelines(non_blank_lines)
- if __name__ == "__main__":
- current_directory = os.getcwd()
- input_file_path = os.path.join(current_directory, 'input.txt') # Replace with the actual input file name
- output_file_path = os.path.join(current_directory, 'output.txt') # Replace with the desired output file name
- remove_blank_lines(input_file_path, output_file_path) # Remove the blank lines
- print("Blank lines removed.") # Terminal Output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement