file reading and task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

file reading and task

whats the problem here? my goal is that when i write at the second input 'sort' so it will save every word of the file in a list and then print a sorted list. and when i write 'last' it will print the last line of the file. (i did 'rev' which reverses the file and it worked.. so no need to help me with this one...) code: take_path_input = input("Enter Path:\n") open_file = open(take_path_input , "r") task_to_do = input("Enter a Task:") if task_to_do == 'sort': for line in open_file: for word in open_file.split(): list_of_sorted_words = list(word) print(sorted(list_of_sorted_words)) elif task_to_do == 'rev': for line in open_file: print(line[::-1]) elif task_to_do == 'last': for line in open_file: print(line[-1]) open_file.close()

26th Sep 2020, 1:01 PM
Yahel
Yahel - avatar
1 Answer
0
take_path_input = input("Enter Path:\n") open_file = open(take_path_input , "r") task_to_do = input("Enter a Task:") if task_to_do == 'sort': list_of_sorted_words = [] #define list for word in open_file.read().split(): #split after reading list_of_sorted_words.append(word) #append, not overrite print(sorted(list_of_sorted_words)) # sorted way elif task_to_do == 'rev': for line in open_file: print(line[::-1]) elif task_to_do == 'last': for line in open_file: print(line[-1]) open_file.close()
26th Sep 2020, 1:30 PM
Jayakrishna 🇮🇳