I'm working on a problem in 'Python for Everyone', chapter 7. The program is meant to take a file and reproduce that file in reverse order of characters. This code makes a list of the characters as they appear, but when I use:
reversedList = sorted(charList, key=itemgetter(1), reverse=True)
I get: IndexError: string index out of range. This is the full code:
from _operator import itemgetter
def main():
file = input("Enter the name of the file to be reversed: ")
file = open(file, "r")
charList = []
char = file.read(1)
charList.append(char[0])
while char != "" :
char = file.read(1)
charList.append(char)
reversedList = sorted(charList, key=itemgetter(1), reverse=True)
file.close()
main()
Please let me know what is going wrong here.