Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Last active May 31, 2024 20:59
Show Gist options
  • Save wilyJ80/5bc8019dd41a50ab30909ee553e34021 to your computer and use it in GitHub Desktop.
Save wilyJ80/5bc8019dd41a50ab30909ee553e34021 to your computer and use it in GitHub Desktop.
Python word sorter: multi-file, supports subdirectories, no loops
#!/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