Last active
May 31, 2024 20:59
-
-
Save wilyJ80/5bc8019dd41a50ab30909ee553e34021 to your computer and use it in GitHub Desktop.
Python word sorter: multi-file, supports subdirectories, no loops
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
#!/usr/bin/env python3 | |
# Sorts words each on its own line, on any number of files | |
from pathlib import Path | |
import timeit | |
def sorter(): | |
files = list(filter(lambda x: x.is_file(), Path('.').rglob('*'))) | |
words = list(map(lambda x: x.read_text(), files)) | |
unique_words = set(''.join(words).split()) | |
sorted_words = sorted(unique_words, key=str.lower) | |
word_string = '\n'.join(sorted_words) | |
filepath = Path('output.txt') | |
filepath.write_text(word_string) | |
if __name__ == "__main__": | |
sorter() | |
# Could've just used `find . -type f | xargs sort -fu > output.txt` smh my head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment