Python - 循环文件移动
Python - Round Robin file move
我正在尝试创建一个 Python 脚本,该脚本以循环方式将文件移动到其中文件数量最少的 DIR 中,以便将源 DIR 中的文件平均分配给两个文件目标目录。
例如:
如果 c:\test 包含:
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 中其中的文件数量,而不是循环分发它们。
这是我的 Ruby 示例:
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
这会输出以下内容:
C:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load([=12=]=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.
文件按照我的意愿移动。
现在这是我的 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')
这个脚本returns这个:
C:\UsersA01\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 脚本哪里出错了,为什么它不循环移动文件?
dir_a
和 dir_b
是在脚本开始时计算的,因此即使您在循环中移动文件,负载也始终相同。
在你的 for
循环中移动它:
dir_a = os.listdir(r'F:\Transcoder\testing\dir_a')
dir_b = os.listdir(r'F:\Transcoder\testing\dir_b')
fox 提案(还有一些其他的小修复,比如不重复路径和使用 "raw" 前缀 (r"the\data"
) 以避免转义反斜杠。
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')
我正在尝试创建一个 Python 脚本,该脚本以循环方式将文件移动到其中文件数量最少的 DIR 中,以便将源 DIR 中的文件平均分配给两个文件目标目录。
例如:
如果 c:\test 包含:
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 中其中的文件数量,而不是循环分发它们。
这是我的 Ruby 示例:
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
这会输出以下内容:
C:\Ruby22-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load([=12=]=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.
文件按照我的意愿移动。
现在这是我的 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')
这个脚本returns这个:
C:\UsersA01\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 脚本哪里出错了,为什么它不循环移动文件?
dir_a
和 dir_b
是在脚本开始时计算的,因此即使您在循环中移动文件,负载也始终相同。
在你的 for
循环中移动它:
dir_a = os.listdir(r'F:\Transcoder\testing\dir_a')
dir_b = os.listdir(r'F:\Transcoder\testing\dir_b')
fox 提案(还有一些其他的小修复,比如不重复路径和使用 "raw" 前缀 (r"the\data"
) 以避免转义反斜杠。
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')