Created
March 24, 2024 05:02
-
-
Save PowerX-NOT/4621fdc7946b4966b3fd84b519bfad52 to your computer and use it in GitHub Desktop.
Sort proprietary-files.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the file path from the user | |
file_path = input("Enter the path to the file: ") | |
# Read the file and store the lines in a list | |
with open(file_path, 'r') as file: | |
lines = file.readlines() | |
# Initialize a dictionary to store libraries under each heading | |
libs_by_heading = {} | |
current_heading = None | |
# Iterate over the lines and categorize them under each heading | |
for line in lines: | |
line = line.strip() | |
if line.startswith('#'): | |
current_heading = line[1:].strip() | |
libs_by_heading[current_heading] = [] | |
elif current_heading: | |
libs_by_heading[current_heading].append(line) | |
# Sort the libraries under each heading | |
for heading in libs_by_heading: | |
libs_by_heading[heading] = sorted(libs_by_heading[heading]) | |
# Write the sorted libraries back to the file | |
with open(file_path, 'w') as file: | |
for heading, libs in libs_by_heading.items(): | |
file.write(f'# {heading}') | |
for lib in libs: | |
file.write(f'{lib}\n') | |
file.write('\n') # Add an empty line after each heading's libraries | |
print("Libraries sorted and written back to the file.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment