Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created July 14, 2023 00:21
Show Gist options
  • Save nick3499/60e38b89c3d4cb5789fb72ba67db4796 to your computer and use it in GitHub Desktop.
Save nick3499/60e38b89c3d4cb5789fb72ba67db4796 to your computer and use it in GitHub Desktop.
Word Counter: os.listdir()
#!/bin/python3
'''Count word total in text file.'''
from os import listdir
print(listdir()) # optional
inp = input('Enter filename: ')
with open(inp) as file:
text = file.read()
replace_mdash = text.replace('—', ' ')
split_text = replace_mdash.split()
print(f'\x1b[0;31mword count\x1b[0m: {len(split_text)}')
@nick3499
Copy link
Author

Used this script to count the total number of words in a text file for my flash fiction hobby.

emdashes were replaced with spaces because they caused the word at each end together to be counted as one.

Since then I use gEdit's word count feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment