Skip to content

Instantly share code, notes, and snippets.

@emres
Created December 22, 2009 15:16
Show Gist options
  • Save emres/261793 to your computer and use it in GitHub Desktop.
Save emres/261793 to your computer and use it in GitHub Desktop.
A very small and yet-to-be-completed Python program for some sound file renaming.
import os
import glob
import re
file_prefix = "prompt_pronunciation_vl_"
file_extension = ".wav"
index_range = [1, 1139]
index = 1
number_of_zeroes = 4
def k(s):
""" Used for human / natural sorting.
For detailed technical discussion see resources below
http://stackoverflow.com/questions/1940056/python-sorts-u11-phrase-1000-wav-before-u11-phrase-101-wav-how-can-i-overcom
http://nedbatchelder.com/blog/200712/human_sorting.html
http://www.codinghorror.com/blog/archives/001018.html
"""
return [w.isdigit() and int(w) or w for w in re.split(r'(\d+)', s)]
files = glob.glob('*.wav')
for file in sorted(files, key=k):
if (index >= index_range[0] and index <= index_range[1]):
print "Current file = " + file
os.rename(file, file_prefix
+ (str(index)).zfill(number_of_zeroes)
+ file_extension)
index = index + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment