I have a bit of code that will try to find an audio file for a corresponding clip that is working well, assuming that the version of the audio file is v01.
audioFullFileName = "%s_v001.wav" % audioCleanFileName
audioCheck = os.path.join( scriptDialog.GetValue("audioDir"), audioFullFileName)
audioFullFileName couold be equal to “…path/shotname_v001.wav”
I would like to make this smarter by adding two things.
First, and probably easier, I would like it to check for version number with padding of 2 or 3.
So _v01 as well as _v001 would be checked.
Secondly, Once a first version is found, I would like it to check for higher versions.
So if there is a path/shotname_v002.wav exists, use that, and check for a v003. Stop when no higher version is found.
can anyone point me in the right direction for a function like this?
Thanks
This is more of a straight python question than a draft question… but the following should do the trick:
def FindAudioFile( prefix, directory ):
allFiles = os.listdir( directory ); # get a list of the files in the directory
largestVersionFound = -1; # we haven't found anything yet
audioFilename = ''
pattern = prefix + '_v([0-9]+).wav' # the parentheses will allow us to get the version number
for file in allFiles:
m = re.match( pattern, file )
if( m != None ):
version = ( int )( m.group( 1 ) )
if( version > largestVersionFound ):
largestVersionFound = version
audioFilename = file # we like this version better
return audioFilename
Note: This function allows for an arbitrary amount of padding and if both 0x and 00x exist, then it uses whichever it finds first. Also, it doesn’t care if there are missing versions… it simply takes the one with the largest version number.