-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-wildcards-by-first-letter.py
46 lines (34 loc) · 1.53 KB
/
sort-wildcards-by-first-letter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import os
import concurrent.futures
def process_files_concurrently(root_directory):
with concurrent.futures.ThreadPoolExecutor() as executor:
file_paths = []
for foldername, subfolders, filenames in os.walk(root_directory):
for filename in filenames:
file_path = os.path.join(foldername, filename)
file_paths.append(file_path)
executor.map(sort_file_by_first_letter, file_paths, file_paths)
def sort_file_by_first_letter(input_file, output_file):
print("processing " + input_file)
if os.path.splitext(input_file)[1] == '.txt':
with open(input_file, 'r', encoding='utf-8') as infile:
lines = infile.readlines()
# Sort lines based on the first letter
sorted_lines = sorted(lines, key=lambda x: x[0].lower())
with open(output_file, 'w', encoding='utf-8') as outfile:
outfile.writelines(sorted_lines)
# Example usage
basefolder = 'X:/dif/stable-diffusion-webui-docker/data/config/auto/extensions/sd-dynamic-prompts/wildcards'
while True:
user_input = input("Are you sure you want to alphabetically sort the files in the directory " + basefolder + " (y/n): " ).lower()
if user_input == 'y':
# Add your code for continuing here
print("Continuing...")
process_files_concurrently(basefolder)
break
elif user_input == 'n':
print("Exiting...")
break # Exit the loop
else:
print("Invalid input. Please enter 'y' to continue or 'n' to exit.")