python - Why is os.rename program not sorting directory -


i have program trying write take large directory (10,000+files inside) , create new sub directories break large directory smaller chunks (of approximately 100 files each).the program have raises no errors when call in terminal, not sort large file... think problem os.rename() dont understand why tried shutil.move() , still had same problem. sorry couldent make code appear in color new site

#!/usr/bin/python import os import glob import sys functools import partial sys.setrecursionlimit(1000)   def mk_osdict(a):     #os.chdir(a)     #grouping files .mol2 endings     os_list =glob.glob("*.mol2")     #making dictionary list of files in directory     os_dict = dict([i,n] i,n in zip(range(len(os_list)),os_list))     return os_dict  dict_os = mk_osdict("decoys")  #function sort files new directories specific size.  def init_path(f):        block = (len(f)/100)+1     #i_lst gives list of number of entries     i_lst = [str(i) in range(block)]     '''paths keys become new directories, values list     files sorted corresponding directory'''     paths = dict(["decoydir"+n.zfill(5),[]] n in i_lst)     lst in paths.values():         while len(lst) <= block:             value in f.values():                 lst.append(value)     x,p in paths:         if not os.path.exists(x):             os.mkdir(x)         else:             pass            index in p:             yield os.rename(index,os.path.join(x,index))  b = init_path(dict_os ) 

you can perform task more using few list manipulations on the files returned glob. creating intermediate data structures makes code more confusing - can directory creation , moves go:

import os import glob

def mk_tree(path):     files = glob.glob(os.path.join(path, "*.mol2"))     chunks = [files[chunk:chunk+100] chunk in range(0, len(files), 100)]     i, chunk in enumerate(chunks):         new_dir = os.path.join(path, "decoydir%05d" % i)         os.mkdir(new_dir)         fn in chunk:             os.rename(fn, os.path.join(new_dir, os.path.basename(fn))) 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -