Hello I have a text file that looks like this:
file '4. Can This Be Love.mp3'
file '3. I Wanna Share It With You.mp3'
file '8. Hold On.mp3'
file '6. Playing With Fire.mp3'
file '1. Take Me To The River.mp3'
file '5. Giving It Up For You.mp3'
file '10. Hooked On You.mp3'
file '9. I Can'\''t Stop.mp3'
file '7. Make It Together.mp3'
file '2. Can'\''t Stop Myself.mp3'
I am trying to sort the file by the beginning string numbers so its in correct order from 1 to 10. My code looks like this:
#open file to read
shopping = open(songInputsFilepath, "r")
#get lines from file
lines = shopping.readlines()
#sort lines
lines.sort()
#close file
shopping.close()
#open file for writing
shoppingwrite = open(songInputsFilepath, "w")
#write sorted lines to file
shoppingwrite.writelines(lines)
#close file for writing
shoppingwrite.close()
It almost works, but misplaces the 10th track, and results in a file sorted like so:
file '1. Take Me To The River.mp3'
file '10. Hooked On You.mp3'
file '2. Can'\''t Stop Myself.mp3'
file '3. I Wanna Share It With You.mp3'
file '4. Can This Be Love.mp3'
file '5. Giving It Up For You.mp3'
file '6. Playing With Fire.mp3'
file '7. Make It Together.mp3'
file '8. Hold On.mp3'
file '9. I Can'\''t Stop.mp3'
Is there some way to tell the lines.sort() function to use a regex expression to only sort each line based on the string which comes before the first 'period' character?