Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: mixed_list_sorter.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- :: Mixed List Sorter ::
- Description:
- This script sorts a mixed list of words and numerical strings in alphanumerical order and saves the sorted content to a new file.
- Requirements:
- - Python 3.x
- - tkinter library (for file dialog)
- Functions:
- - select_file(): Opens the file manager in the current working directory and lets the user select the file to sort.
- - sort_file(input_file_path): Sorts the lines of the input file in alphanumerical order and saves the sorted content to a new file.
- Usage:
- 1. Run the script.
- 2. The file manager will open, allowing you to select a text file to sort.
- 3. After selecting the file, the script will sort the content and save the sorted content to a new file named "sorted_list.txt" in the same directory as the original file.
- Additional Notes:
- - The script can handle both words and numerical strings mixed together in the input file.
- - The sorted content is saved with headers indicating the sorted number list and the sorted word list in the output file.
- """
- # Get Essential Imports
- import os
- from tkinter import filedialog
- # Function to get user selected file to sort
- def select_file():
- """
- Open the file manager in the current working directory and let the user select the file to sort.
- Returns:
- str: The selected file path.
- """
- cwd = os.getcwd()
- file_path = filedialog.askopenfilename(
- initialdir=cwd,
- title="Select file",
- filetypes=(("Text files", "*.txt"), ("All files", "*.*")),
- )
- return file_path
- # Function to sort the selected file in alphanumerical order
- def sort_file(input_file_path):
- """
- Sort the lines of the input file in alphanumerical order and save the sorted content to a new file.
- Args:
- input_file_path (str): The path of the input file to be sorted.
- """
- try:
- with open(input_file_path, "r", encoding="utf-8") as file:
- lines = file.readlines()
- # Separate lines into numbers and letters
- numbers = [line.strip() for line in lines if line.strip().isdigit()]
- letters = [line.strip() for line in lines if not line.strip().isdigit()]
- # Sort numbers and letters separately
- numbers.sort(key=int)
- letters.sort()
- # Merge sorted numbers and letters with headers
- sorted_lines = (
- ["Sorted number list:\n"] + numbers + ["", "Sorted word list:\n"] + letters
- )
- # Dynamically name the output file based on the input fil name
- base_name = os.path.basename(input_file_path)
- dir_name = os.path.dirname(input_file_path)
- output_file_path = os.path.join(dir_name, f"sorted_{base_name}")
- with open(output_file_path, "w", encoding="utf-8") as file:
- file.write("\n".join(sorted_lines))
- print(f"File '{input_file_path}'\nHas been sorted successfully.")
- print(f"\nSorted content has been written to:\n'{output_file_path}'.")
- except Exception as e:
- print(f"\nAn error occurred: {e}\n")
- def main():
- """
- Main function to run the script.
- """
- selected_file = select_file()
- if selected_file:
- sort_file(selected_file)
- else:
- print("\nNo file selected!\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement