Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

simple file copy program

Name: Anonymous 2010-01-18 23:35

need a bash script or python to copy files in batches of 500 and put them into subdirectories

there can be any number of files (I have 15k), and the filenames are irrelevant, and the names of the subdirectories that are created to house the files is irrelevant

here's what I was trying to get work in python but it
errors out at file 339, don't know why



def BreakUp(indir):
   
    biglist = glob.glob('*.mp3')
    dircount = 0
    filecount = 0

    while biglist:
        dircount += 1
        os.mkdir('subdir'+str(dircount))
        for i in range(500):   
            filecount += 1
            try:
                src = biglist[filecount].translate("(copy)")
                os.system ("mv"+ " " + src + " subdir"+ str(dircount))          
                biglist.remove(src) 
            except IndexError:
                raise IndexError, "could not return item " + str(filecount) + " from list " + str(biglist)
        continue
               
           
       

if __name__ == '__main__':
    BreakUp(".")

Name: Anonymous 2010-01-19 19:42

>>3,10,19
Just to dispel the myth that this is much harder in Python, here's the identical code in Python: (I did not test it)

import os
f = [i for i in os.listdir('.') where i[:-4].lower() == ".mp3"]
for i in range(0, len(f)/500):
  os.mkdir(str(i))
n = 0
for i in f:
  os.rename(i, str(n/500) + "/" + f)
  n += 1


Notable overhead as compared to perl: namespaced os functions; no built-in globbing operation; forced int to string conversion; and no post-increment.

Note that I added lower() to the .mp3 filter. The path concatenation should also be done with os.path.join() instead of doing string concatenation with a forward slash.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List