40.1k views
4 votes
Write a complete Python program that opens a file called 'myfile', reads the entire contents of the file into a string s, determines the number of words in myfile and prints that number on a line, then prints out all words in myfile in alphabetical order, each word on a separate line.

1 Answer

3 votes

Answer:

myfile = open("myfile","r").readlines();

myfile_altered = "".join(myfile.split("\\"))

length = len(myfile_altered)

myfile_sort = " ".join(myfile).split(" ")

myfile.sort()

print(length)

print(myfile)

Step-by-step explanation:

I read the file and split the text by a space, and just sorted the list using `.sort()`

Should work (didnt test it). If it doesn't, just comment on this answer and I will debug it.

User Ramiro Ramirez
by
4.0k points