[Tutor] automatic output file naming scheme
Emile van Sebille
emile at fenx.com
Sun Mar 28 18:17:43 CEST 2010
On 3/28/2010 8:44 AM kevin parks said...
> okay. I got the subprocess bit to work and i have os walk doing its walk. But now for something i did not think about until i started to think about how to fit these two bits to work together.
> os walk is going to traverse my dirs from some given starting point and process files that it finds that fit my criteria. So i my case it will look for all sound files in a given directly structure and then call sox and do a conversion. This part i can already probably do just by combining the working code that i have … but now i need to have some kind of automagic naming scheme that creates an output file name that is based on the input name but is unique, either adding a number or suffix before the file extension, or even a time stamp. Since we want to create file names that are unique and not clobber existing files, but also will tells us something meaningful about how the file was created so that finding:
> foo01.aif or foo.1.aif would yield something like foo-out01.aif or foo01-out.aif or something similar.
> How can you do such a thing in python? is there some elegant way to take the input file name and combine it with some other string to create the output file name?
So you've really got two issues -- building a new name to use, and
confirming it's unique.
You can build a new name with the replace method of strings or
concatenating pieces of the new name using join or string interpolation.
>>> sourcefile = "/home/someone/somedir/foo01.aif"
>>> sourcefile.replace(".aif","-%03d.aif" % sequence)
os.path has some functions you may find helpful:
More information about the Tutor