2016-11-08 4 views
0

ラウンドロビンのファイルを最小限の量のファイルに移動するPythonスクリプトを作成しようとしています。そのため、ファイルがソースDIRと2つのファイルに均等に分散されますターゲットディレクトリ。例えばPython - ラウンドロビンファイルの移動

C場合:\テストが含まれています

test_1.txt test_2.txt test_3.txt test_4.txt

私はこれらのtest_1.txtとtest_3をしたいです。 txtをc:\ test \ dir_aに移動し、test_2.txtとtest_4.txをc:\ test \ dir_bに移動します。

私はこれをRubyで成功させることができましたが、スクリプトを実行するときにPythonでこれを実行しようとすると、すべてのファイルが最小限の量のファイルをDIRに移動しますラウンドロビン。私は彼らがしたいよう

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 to DIR A 
Files successfully moved to staging area. 
F:/Transcoder/testing/prep/test_2.txt moved to DIR B 
Files successfully moved to staging area. 
F:/Transcoder/testing/prep/test_3.txt moved to DIR A 
Files successfully moved to staging area. 
F:/Transcoder/testing/prep/test_4.txt moved to DIR B 
Files successfully moved to staging area. 

ファイルが移動します。これは、次の出力

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 do |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 to DIR A" 

     elsif loadmin == nc_2 

      FileUtils.mv Dir.glob("#{f}"), watchfolder_2 

      puts "#{f} moved to DIR B" 

     end 

     puts 'Files successfully moved to staging area.' 

    end 

    else 
     puts 'No valid files found' 
    end 
end 
check_file 

は、ここに私のRubyの一例です。今ここに

は私のPythonスクリプトです:

import shutil 
from 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') 

    for 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') 

このスクリプトは、これを返します。

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 to DIR A 
Moving F:\Transcoder\testing\prep\test_2.txt to DIR A 
Moving F:\Transcoder\testing\prep\test_3.txt to DIR A 
Moving F:\Transcoder\testing\prep\test_4.txt to DIR A 
私はPythonスクリプトと間違ってんだ、なぜそれがラウンドでファイルを移動されていない

ロビン?

答えて

1

dir_aおよびdir_bは、スクリプトの開始時に計算されるため、ループ内のファイルを移動しても負荷は常に同じになります。

あなたforループ内での移動、これを:パスを繰り返し、antislashesをエスケープ避けるために、「生」の接頭辞(r"the\data")を使用していないようにもいくつかの他の小さな修正を

dir_a = os.listdir(r'F:\Transcoder\testing\dir_a') 
dir_b = os.listdir(r'F:\Transcoder\testing\dir_b') 

キツネの提案(、

import shutil 
from 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:   

    for 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') 
+0

私は質問があります。t_aとt_bのパスの前にある "r"はどういう意味ですか? – Lewis909

+0

私の編集を参照してください。あなたの場合。 –

関連する問題