As it is known, MP3 contains the audio track itself and some metadata such as the title of the track, album, band's name and some other information.

And recently I got a necessity to fix the order of the tracks related to an audio book. In this case, the order of tracks matters. But originally, the files were not be marked by track numbers, so the players keep arbitrary order of the played files. I decided to apply a Python script to resolve this issue and I found a tool eyeD3 that allows to read and modify the metadata of audiofiles.

eyeD3 installation:

pip install eyeD3

In my particular case, the order of track must be the same as the alphabet order of the file names. So I wrote and ran this script:

import os
import eyed3

path = '/path/to/files/'

names = os.listdir(path)
names.sort()
for num, name in enumerate(names, 1):
    audiofile = eyed3.load(os.path.join(path, name))
    audiofile.tag.track_num = num
    audiofile.tag.save()

It worked, the tracks go in the order I need. More about eyeD3 you can find on the official documentation.