Advertisement
Python253

remove_blank_lines

Mar 1st, 2024
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: remove_blank_lines.py
  3. # Author: Jeoi Reqi
  4. # This script removes blank lines from a text file, preserving unique lines in sorted order & saves to file.
  5.  
  6. import os
  7.  
  8. # Function to remove blank lines from text
  9. def remove_blank_lines(input_file, output_file):
  10.     with open(input_file, 'r', encoding='utf-8') as file:
  11.         lines = file.readlines()
  12.  
  13.     non_blank_lines = [line for line in lines if line.strip() != '']
  14.     # Write Data To File
  15.     with open(output_file, 'w', encoding='utf-8') as output:  # Use utf-8 Encoding
  16.         output.writelines(non_blank_lines)
  17.  
  18. if __name__ == "__main__":
  19.     current_directory = os.getcwd()
  20.     input_file_path = os.path.join(current_directory, 'input.txt')   # Replace with the actual input file name
  21.     output_file_path = os.path.join(current_directory, 'output.txt')   # Replace with the desired output file name
  22.  
  23.     remove_blank_lines(input_file_path, output_file_path)  # Remove the blank lines
  24.  
  25.     print("Blank lines removed.")   # Terminal Output
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement