I implemented this in two parts. The first part created the required directory structure:
import os
workPath = "/tmp/test/"
for a in range (0, 16):
os.mkdir(workPath+hex(a)[2:])
for b in range (0, 16):
os.mkdir(workPath+"/"+hex(a)[2:]+"/"+hex(b)[2:])
for c in range (0, 16):
os.mkdir(workPath+"/"+hex(a)[2:]+"/"+hex(b)[2:]+"/"+hex(c)[2:])
The next part was to move files to the corresponding directories. This was also quite straightforward but I had problem with os.rename and I had to replace it with shutil.move. The original script worked fine on my home computer but the development server had the source and destination directories in different filesystems. Here is the final version:
import os
import shutil
sourceDir = "/tmp/files/"
destDir = "/tmp/test/"
filenames = os.listdir(sourceDir)
for filename in filenames:
sourceFile = sourceDir+filename
print(sourceFile)
destFile = destDir+filename[0]+"/"+filename[1]+"/"+filename[2]+"/"+filename
print(destFile)
shutil.move(sourceFile, destFile)
These did not take too long to make and most likely someone who remembers Python libraries better would have done it in a fraction of time.