Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1.  
  2. def sort(file_path):
  3. '''
  4. This function gets a text(str) and print a list with
  5. all unique words from the str but sorted scendingly.
  6. '''
  7. with open(file_path, "r") as file:
  8. unique_words_list = []
  9. add_word = ''
  10. for line in file:
  11. for word in line.split():
  12. for letter in word:
  13. if letter.isalpha:
  14. add_word += letter
  15. if add_word not in unique_words_list:
  16. unique_words_list.append(add_word)
  17. add_word = '' #reset word to add
  18. return(unique_words_list.sort())
  19.  
  20. def rev(file_path):
  21. '''
  22. This function gets a text(str) and print
  23. every line from the end to the start
  24. '''
  25. with open(file_path, "r") as file:
  26. for line in file:
  27. return(line[::-1])
  28.  
  29.  
  30. def last(file_path):
  31. '''
  32. This function gets a text(str) and a n number from the user
  33. a prints the last n lines from the str
  34. every line from the end to the start
  35. '''
  36. with open(file_path, "r") as file:
  37. last = int(input("Enter a number: "))
  38. lines = file.readlines()
  39. for i in range(-last, 0):
  40. return(lines[i])
  41.  
  42.  
  43. file_path = input("Enter a file path: ")
  44. task = input("Enter a task: ")
  45. if task == "sort":
  46. print_me = sort(file_path)
  47. elif task == "rev":
  48. print_me = rev(file_path)
  49. elif task == "last":
  50. print_me = last(file_path)
  51. print(print_me)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement