Reputation: 1
Based on this great piece of code: GitHub Splitter.py
I would like to work on files and not on folders.
The code is so short:
#gadgetmiser's idempotent splitter
import os
from shutil import move
wd = os.getcwd()
dirs = os.listdir(wd)
lastitemindex = len(dirs) - 1
drive = wd.split(':')[0]
results = ':\\c64Romset_output\\'
filesperfolder = 255
letterstodisplay = 15
firstfn_2l = dirs[0][:letterstodisplay].split("(")[0].strip().upper()[:4]
lastfn_2l = dirs[:filesperfolder - 1][-1][:letterstodisplay].split("(")[0].strip().upper()[:4]
firsttarget = (firstfn_2l + " to " + lastfn_2l).strip()
os.mkdir(drive + results)
os.mkdir(drive + results + firsttarget)
log_output = open(drive + results + "log_output.txt","w+")
tally = 0
counter = 0
target = firsttarget
for folder in dirs:
tally += 1
if counter == filesperfolder:
p1 = folder[:letterstodisplay].split("(")[0].upper().strip()[:4]
folderpos = dirs.index(folder)
try:
p2 = dirs[folderpos + filesperfolder - 1][:letterstodisplay].split("(")[0].upper().strip()[:4]
except IndexError:
p2 = dirs[lastitemindex][:letterstodisplay].split("(")[0].upper().strip()[:4]
newdir = (p1 + " to " + p2).strip()
print(str(tally) + " files processed ... making new dir called: " + newdir)
target = newdir
counter = 0
newfname = folder.split("(")[0].strip()
move(folder, drive + results + target + "\\" + newfname)
print("Moved " + folder + " to " + drive + results + target + " as " + newfname)
log_output.write("\n" + "Moved " + folder + " to: ** " + target + " **" + " as " + newfname)
counter += 1
print(str(tally) + ' files processed ... ALL DONE')
log_output.write("\n" + str(tally) + " files processed.")
log_output.close()
And this work when we want to move a lot of folders into folders (max 255 folders per folder) However I need to work on Files directly, as per example to have 10.000 files and to split into folders with max 255 files each, like the code do for the folders.
At the moment the script work like this, example of 1.000 folders: that folders are automatically splitted in 4 folders (255, 255, 255, 235)
I would like to have the same per files, in example if we have 1.000 files, I would like to split to 4 folders (255, 255, 255, 235 files per each one principal folder)
While I'm totally newbie to Python, I hope someone should let me know what to change in code. Many thanks.
I just tried to modify any 'folder' entry in code with 'files' no errors here but no folders were created.
Upvotes: 0
Views: 28