This Python program assumes that the source files are in dataand that the new directory structure must be in target(and that it already exists).
The key point is that it os.path.walkwill navigate through the directory structure dataand call myVisitorfor each file.
import os
import os.path
sourceDir = "data"
targetDir = "target"
def myVisitor(arg, dirname, names):
for file in names:
bandDir = file.split("-")[0]
newDir = os.path.join(targetDir, bandDir)
if (not os.path.exists(newDir)):
os.mkdir(newDir)
newName = os.path.join(newDir, file)
oldName = os.path.join(dirname, file)
os.rename(oldName, newName)
os.path.walk(sourceDir, myVisitor, None)
source
share