Skip to content

Instantly share code, notes, and snippets.

@jb0hn
Last active September 9, 2018 19:55
Show Gist options
  • Save jb0hn/7662bda6b74870bbb4cb328151fe3a44 to your computer and use it in GitHub Desktop.
Save jb0hn/7662bda6b74870bbb4cb328151fe3a44 to your computer and use it in GitHub Desktop.
Switch between uppercase, lowercase and capitalized file names
# get the file's location
abs_file_loc = input("\nInput absolute path for the file you want to convert: ")
# open the file, read and return a list containing all lines
with open(abs_file_loc, 'r') as tht:
input_file = tht.readlines();
# get the line number to conversion
while True:
line_num = int(input("\nPass the line number you want to convert: "))
if line_num in range(1, len(input_file)+1): # make sure that the passed line number is in the list containing lines from the file
# convert line number from human (1 to ...) to python (0 to ...)
line_num -= 1
# ask user if he/she has selected proper line
while True:
print("Is the line below the line you want to convert? (Y/N):\n\n{}".format(input_file[line_num]))
permission = input("Answer: ")
permission = permission.lower()
# check if answer is 'y' or 'n'
if permission == 'y' or permission =='n':
break
# finish checking if user accept the line or repeat the choosing
if permission == "y":
break
elif permission == "n":
continue
# select type of conversion
while True:
convert_mode = int(input("""
Choose the mode of conversion:
[1] lowercase
[2] UPPERCASE
[3] Capitalized
Mode: """))
if convert_mode in range(1, 4):
break
# convert the line
if convert_mode == 1:
input_file[line_num] = input_file[line_num].lower()
elif convert_mode == 2:
input_file[line_num] = input_file[line_num].upper()
else:
input_file[line_num] = input_file[line_num].capitalize()
# replace the original line with converted one
with open(abs_file_loc, 'w') as output_file:
output_file.writelines(input_file)
print("\nFinished.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment