Advertisement
Python253

sort_user_list

May 17th, 2024
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: sort_user_list.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script sorts a list of user data alphabetically and saves the sorted list to an output file.
  10.  
  11. Functions:
  12.    - line():
  13.        Prints a line of underscores for formatting purposes.
  14.    - sort_user_list(local_input_filename, local_output_filename):
  15.        Reads a list of user data from an input file, sorts it alphabetically, and saves the sorted list to an output file.
  16.  
  17. Requirements:
  18.    - Python 3.x
  19.  
  20. Usage:
  21.    - To use this script, call the sort_user_list function with the input and output filenames as arguments.
  22.  
  23. Additional Notes:
  24.    - Make sure the input file exists and is accessible.
  25.    - The output file will be overwritten if it already exists.
  26. """
  27.  
  28. def line():
  29.     """Prints a line of underscores for formatting purposes."""
  30.     print("_" * 50, "\n")
  31.    
  32. def sort_user_list(local_input_filename, local_output_filename):
  33.     """
  34.    Reads a list of user data from an input file, sorts it alphabetically, and saves the sorted list to an output file.
  35.  
  36.    Args:
  37.        local_input_filename (str): The filename of the input file containing the unsorted user data.
  38.        local_output_filename (str): The filename of the output file where the sorted user data will be saved.
  39.    """
  40.     line()
  41.     print("\tSorting user list...\n\tThis may take some time to process.")
  42.     line()
  43.     # Read the user list from the input file
  44.     with open(local_input_filename, 'r', encoding='utf-8') as infile:
  45.         user_list = [line.strip() for line in infile]
  46.  
  47.     # Sort the user list alphabetically
  48.     sorted_user_list = sorted(user_list)
  49.  
  50.     # Write the sorted user list to the output file
  51.     with open(local_output_filename, 'w', encoding='utf-8') as outfile:
  52.         for user in sorted_user_list:
  53.             outfile.write(user + '\n')
  54.    
  55.     print(f"- Sorted user list saved to: '{local_output_filename}' -")
  56.     line()
  57.     print("\tExiting Program...\tGoodBye!\n")
  58.     line()
  59.  
  60. if __name__ == "__main__":
  61.     global_input_filename = 'input.txt'  # Replace with your list filename
  62.     global_output_filename = 'sum_list.txt'
  63.     sort_user_list(global_input_filename, global_output_filename)
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement