Skip to content

Instantly share code, notes, and snippets.

@philipsinnott
Created February 7, 2024 13:26
Show Gist options
  • Save philipsinnott/a1e4371fdb8e44c4fb819e9d022004eb to your computer and use it in GitHub Desktop.
Save philipsinnott/a1e4371fdb8e44c4fb819e9d022004eb to your computer and use it in GitHub Desktop.
Capitalize the first letter of a list of words
import sys
def display_help():
print("USAGE: python ~/tools/capitalizer.py input.txt output.txt\n")
print("ARGS:")
print(" [input_file] --> Input file containing a list of words")
print(" [output_file] --> Output file to write the capitalized words")
# Display help if --help flag is provided or if number of arguments is less than 2
if "--help" in sys.argv or len(sys.argv) < 3:
display_help()
sys.exit()
input_file = sys.argv[1]
output_file = sys.argv[2]
# Read words from input file, capitalize if it's not a number and the first character is not already uppercase, and write to output file
with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
first_word = True
for line in f_in:
word = line.strip()
if word and not word.isdigit() and not word[0].isupper():
word = word.capitalize()
if first_word:
first_word = False
else:
f_out.write('\n')
f_out.write(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment