Scripts to manage files

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值