Question: Python The file unsorted_fruits.txt (which is available in the resources section of this unit in the resources section) contains a list of 26 fruits, each
Python
The file unsorted_fruits.txt (which is available in the resources section of this unit in the resources section) contains a list of 26 fruits, each one with a name that begins with a different letter of the alphabet.
Write a program named sort_fruits.py that reads in the fruits from the file unsorted_fruits.txt and writes them out in alphabetical order to a file named sorted_fruits.txt.
You must incorporate the use of a list, for loop and / or while loop.
You can use the following fragment of code the open and read in the data from the unsorted_fruits.txt file. You will need to determine how to use a list structure to sort the items and how to write the data back out into the sorted_fruits.txt file.
infile = open("unsorted_fruits.tex", "r") outfile=open("sorted_fruits.txt","w") fruit=infile.read(50) outfile.write(fruit) print (fruit) infile.close() outfile.close()
In using this code fragment, once you have read a line from the file into the variable text, you must then determine how to load it into a list structure. Also keep in mind that you may need to include the directory path when opening files so this may need to be included in your open statement.
LIST
papaya
kiwifruit
zapote blanco
huckleberry
banana
fig
lime
xigua
vanilla
yiessas
tamarind
umkolo
quince
apple
imbu
elderberry
juneberry
mango
strawberry
nectarine
date
cherry
orange
watermelon
grape
raspberry
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
