Here are several Scripts to manage files in python.
1 - To put files in to different directories evenly.
The .py script shall be the files directory to be handled.
# -*- coding: utf-8 -*-
"""
Seperate files in differnt directories evenly.
torrents for example.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import shutil
def move_files_to_dir(file_list, to_dir_path):
if not os.path.exists(to_dir_path):
os.makedirs(to_dir_path)
else:
return
for f in file_list:
shutil.move(f, os.path.join(to_dir_path, f))
def main():
files = filter(lambda x:x.endswith('.torrent'), os.listdir(os.getcwd()))
files_number = len(files)
if files_number == 0:
return
if files_number < 100:
start = 1
end = files_number+1
dir_name = "%d-%d"%(start, end)
move_files_to_dir(files, dir_name)
return
file_list = []
for i in range(files_number):
file_list.append(files[i])
# 0-99
if (i+1)%100 == 0 and i>0:
start = i+2-100
end = i+1
dir_name = "%d-%d"%(start, end)
move_files_to_dir(file_list, dir_name)
file_list = []
if i >= (files_number//100)*100:
files = filter(lambda x:x.endswith('.torrent'), os.listdir(os.getcwd()))
start = i+1
end = files_number+1
dir_name = "%d-%d"%(start, end)
move_files_to_dir(files, dir_name)
return
if __name__ == '__main__':
main()