<commit_before>#!/usr/bin/python
#Python script to rename mp3 files according to the format
#"Track-number Track-name.mp3", for example: 02 Self Control.mp3
#Note: Tracks must have valid ID3 data for this to work - python-mutagen is required.
#By <NAME>

import os
import sys
from mutagen.id3 import ID3, ID3NoHeaderError

def usage() :
    print('''Usage:
  mp3-file-renamer.py <path to music>''')

#Get music directory
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
else :
    if os.path.exists(args[1]) : musicDir = args[1]
    else :
        usage()
        os._exit(0)

#Get titles and track numbers for songs
musicFiles = []
trackNums = []
titles = []

for root, dirs, files in os.walk(musicDir, topdown=False):
    for name in files:
        if name.find(".mp3") != -1 : 
            musicFiles.append(os.path.join(root, name))

for x in musicFiles :
    try :
        audio = ID3(x)
        titles.append(str(audio["TIT2"].text[0]))
        trackNums.append(str(audio["TRCK"].text[0]))
    except (ID3NoHeaderError, KeyError) :
        musicFiles.remove(x)

#Add leading 0 if missing
#Remove number of tracks per album if it exists
for x in trackNums :
    if len(x.split("/")[0]) == 1 : trackNums[trackNums.index(x)] = "0" + x
    trackNums[trackNums.index(x)] = x.split("/")[0]

if (len(trackNums) != len(titles)) or (len(trackNums) == len(titles) == 0) :
    print("Music files not found or improperly tagged. Unable to continue.")
    os._exit(0)
else :
    #Start renaming
    def getPath(origSong) :
        return origSong.rfind("/") + 1
 
    counter = 0
    for x in musicFiles :
        path = x[:getPath(x)]
        os.rename(x, path + trackNums[counter] + " " + titles[counter] + ".mp3")
        counter += 1<commit_msg>mp3-files-renamer.py - improve getting the path to the file<commit_after>#!/usr/bin/python
#Python script to rename mp3 files according to the format
#"Track-number Track-name.mp3", for example: 02 Self Control.mp3
#Note: Tracks must have valid ID3 data for this to work - python-mutagen is required.
#By <NAME>

import os
import sys
from mutagen.id3 import ID3, ID3NoHeaderError

def usage() :
    print('''Usage:
  mp3-file-renamer.py <path to music>''')

#Get music directory
args = sys.argv
if (len(args) != 2) or (args[1] == '-h') or (args[1] == '--help') :
    usage()
    os._exit(0)
else :
    if os.path.exists(args[1]) : musicDir = args[1]
    else :
        usage()
        os._exit(0)

#Get titles and track numbers for songs
musicFiles = []
trackNums = []
titles = []

for root, dirs, files in os.walk(musicDir, topdown=False):
    for name in files:
        if name.find(".mp3") != -1 : 
            musicFiles.append((root, os.path.join(root, name)))

for x in musicFiles :
    try :
        audio = ID3(x[1])
        titles.append(str(audio["TIT2"].text[0]))
        trackNums.append(str(audio["TRCK"].text[0]))
    except (ID3NoHeaderError, KeyError) :
        musicFiles.remove(x)

#Add leading 0 if missing
#Remove number of tracks per album if it exists
for x in trackNums :
    if len(x.split("/")[0]) == 1 : trackNums[trackNums.index(x)] = "0" + x
    trackNums[trackNums.index(x)] = x.split("/")[0]

if (len(trackNums) != len(titles)) or (len(trackNums) == len(titles) == 0) :
    print("Music files not found or improperly tagged. Unable to continue.")
    os._exit(0)
else :
    #Start renaming
    counter = 0
    for x in musicFiles :
        os.rename(x[1], x[0] + "/" + trackNums[counter] + " " + titles[counter] + ".mp3")
        counter += 1
{'fraction_non_alphanumeric': 0.15403969807100923, 'fraction_numerical': 0.02040816326530612, 'mean_word_length': 3.0612939841089672, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '194003', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1253, 'n_words': 360}