python - Writing files to directories in a round robin pattern -


i'm looking solution, within python, write generated files in round robin fashion set of target directories, such files evenly distributed.

so if there 5 target directories: d1, d2, d3, d4 , d5

and constant stream of generated files (f1 .... fn), directories written follows:-

d1: f1, f6, f11 ... etc d2: f2, f7, f12 ... etc d3: f3, f8, f13 ... etc d4: f4, f9, f14 ... etc d5: f5, f10, f15 ... etc 

ideally there may python lib out there allows ...

thanks

use itertools.cycle next directory next file:

it = itertools.cycle( ['d1', 'd2', 'd3'] )  print next(it) print next(it) print next(it) print next(it) print next(it) print next(it) 

result

d1 d2 d3 d1 d2 d3 

it = itertools.cycle( ['d1', 'd2', 'd3'] )  x in ['f1', 'f2', 'f3', 'f4' , 'f5', 'f6']:                           print x, 'write in', next(it) 

result

f1 write in d1 f2 write in d2 f3 write in d3 f4 write in d1 f5 write in d2 f6 write in d3 

edit: kevin said

dirs = ['d1', 'd2', 'd3'] files = ['f1', 'f2', 'f3', 'f4' , 'f5', 'f6']  f,d in zip(files, itertools.cycle(dirs)):    print f, 'write in', d 

result

f1 write in d1 f2 write in d2 f3 write in d3 f4 write in d1 f5 write in d2 f6 write in d3 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -