I would like to sort the file 'shopping.txt' in alphabetical order
shopping = open('shopping.txt')
line=shopping.readline()
while len(line)!=0:
print(line, end ='')
line=shopping.readline()
#for eachline in myFile:
# print(eachline)
shopping.close()
解决方案
Just to show something different instead of doing this in python, you can do this from a command line in Unix systems:
sort shopping.txt -o shopping.txt
and your file is sorted. Of course if you really want python for this: solution proposed by a lot of other people with reading file and sorting works fine