Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Repeated code is usually considered bad style (DRY is the buzzword). Also I usually try not to interleave functionality.</p> <p>For the "design" of this little script I would first walk the directories and create a large list of all audio files and directories. Then I write a function handling the changing of one the items in the list and create another list using <code>map</code>. Now you have a <code>current</code> and a <code>want</code> list. Then I would <code>zip</code> those lists together and rename them all.</p> <p>If your Music Library is really huge, you can use itertools, so you don't have large lists in memory but iterators (only one item in memory at once). This is really easy in python: use <code>imap</code> instead of <code>map</code> and <code>izip</code> instead of <code>zip</code>.</p> <p>To give you an impression and a few hints to useful functions, here is a rough sketch of how I would do it. (Warning: untested.)</p> <pre><code>import os import sys words = ['A','An','The','Of','For','To','By','Or','Is','In','Out','If','Oh','And','On','At'] wantWords = map(str.lower, words) def main(args): rootdir = args[1] files = findFiles(rootdir) wantFiles = map(cleanFilename, files) rename(files, wantFiles) def findFiles(rootdir): result = [] for root, subFolders, files in os.walk(rootdir): for filename in files: result.append(os.path.join(root, filename)) return result def cleanFilename(filename): # do replacement magic def rename(files, wantFiles): for source, target in zip(files, wantFiles): os.rename(source, target) if __name__ == '__main__': main(sys.argv) </code></pre> <p>The advantage is that you can see in the <code>main()</code> what is happening without looking into the details of the functions. Each function does different stuff. On only walks the filesystem, on only changes one filename, one actually renames files.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload