ruby - Python - Round Robin file move -
i trying create python script moves files in round robin dir has least amount of files in files equally distributed source dir 2 target dir's.
for example:
if c:\test contains:
test_1.txt test_2.txt test_3.txt test_4.txt
i want these test_1.txt , test_3.txt moved c:\test\dir_a , test_2.txt , test_4.tx moved c:\test\dir_b.
i have been able in ruby, when try in python when script runs moves files dir least least amount of files in instead of distributing them in round robin.
here ruby example:
require 'fileutils' def check_file watchfolder_1 = 'f:/transcoder/testing/dir_a/' watchfolder_2 = 'f:/transcoder/testing/dir_b/' if !dir.glob('f:/transcoder/testing/prep/*.txt').empty? dir['f:/transcoder/testing/prep/*.txt'].each |f| node_1 = dir["#{watchfolder_1}"+'*'] node_2 = dir["#{watchfolder_2}"+'*'] nc_1 = node_1.count nc_2 = node_2.count loadmin =[nc_1,nc_2].min #puts loadmin if loadmin == nc_1 fileutils.mv dir.glob("#{f}"), watchfolder_1 puts "#{f} moved dir a" elsif loadmin == nc_2 fileutils.mv dir.glob("#{f}"), watchfolder_2 puts "#{f} moved dir b" end puts 'files moved staging area.' end else puts 'no valid files found' end end check_file
this outputs following:
c:\ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=argv.shift) f:/ruby/transcode_engine/test.rb f:/transcoder/testing/prep/test_1.txt moved dir files moved staging area. f:/transcoder/testing/prep/test_2.txt moved dir b files moved staging area. f:/transcoder/testing/prep/test_3.txt moved dir files moved staging area. f:/transcoder/testing/prep/test_4.txt moved dir b files moved staging area.
the files move want them to.
now here python script:
import shutil glob import glob import os.path dir_a = os.listdir('f:\\transcoder\\testing\\dir_a\\') dir_b = os.listdir('f:\\transcoder\\testing\\dir_b\\') t_a = 'f:\\transcoder\\testing\\dir_a\\' t_b = 'f:\\transcoder\\testing\\dir_b\\' if os.listdir('f:\\transcoder\\testing\\prep\\'): prep = glob('f:\\transcoder\\testing\\prep\\*.txt') file in prep: ac = len(dir_a) bc = len(dir_b) load = [ac, bc] if min(load) == ac: print('moving' + file + 'to dir a') shutil.move(file, t_a) elif min(load) == bc: print('moving' + file + 'to dir b') shutil.move(file, t_b) else: print('no files')
this script returns this:
c:\users\3a01\appdata\local\programs\python\python35-32\python.exe f:/projects/python_transcoder/test_2.py moving f:\transcoder\testing\prep\test_1.txt dir moving f:\transcoder\testing\prep\test_2.txt dir moving f:\transcoder\testing\prep\test_3.txt dir moving f:\transcoder\testing\prep\test_4.txt dir
where going wrong python script, why not moving files in round robin?
dir_a
, dir_b
computed @ start of script load identical if move files in loop.
move in for
loop:
dir_a = os.listdir(r'f:\transcoder\testing\dir_a') dir_b = os.listdir(r'f:\transcoder\testing\dir_b')
fox proposal (with other small fixes well, not repeating paths , using "raw" prefix (r"the\data"
) avoid escaping antislashes.
import shutil glob import glob import os.path t_a = r'f:\transcoder\testing\dir_a' t_b = r'f:\transcoder\testing\dir_b' prep = glob('f:\\transcoder\\testing\\prep\\*.txt') if prep: file in prep: dir_a = os.listdir(t_a) dir_b = os.listdir(t_b) ac = len(dir_a) bc = len(dir_b) load = [ac, bc] if min(load) == ac: print('moving' + file + 'to dir a') shutil.move(file, t_a) else: print('moving' + file + 'to dir b') shutil.move(file, t_b) else: print('no files')
Comments
Post a Comment