python实现文件分组复制到不同目录的例子

yipeiwu_com7年前Python基础

场景:某个文件夹下面包含数量巨大的文件,需求需要将这些文件按组(比如5000个一组)存放到不同的目录中去。

复制代码 代码如下:

# Filename: CopyFiles.py
import os
import os.path

folder_capacity = 20

def copy_files(src_dir, dest_dir):
    count = 0
    current_folder = ''

    for item in os.listdir(src_dir):
        abs_item = os.path.join(src_dir, item)
        if os.path.isfile(abs_item):
            count += 1
            if count%folder_capacity == 1:
                current_folder = os.path.join(dest_dir, str(count/folder_capacity))
                os.mkdir(current_folder)
            open(os.path.join(current_folder, item), 'wb').write(open(abs_item, 'rb').read())

copy_files(r'C:\\src', r'C:\\dest')

相关文章

python pyinstaller打包exe报错的解决方法

python pyinstaller打包exe报错的解决方法

今天用python 使用pyinstaller打包exe出现错误 环境pyqt5 + python3.6 32位 在导入pyqt5包之前加上如下代码 import sys impo...

Python实现的列表排序、反转操作示例

本文实例讲述了Python实现的列表排序、反转操作。分享给大家供大家参考,具体如下: 排序: 使用sorted方法和列表的sort方法: sorted方法适用范围更广,sort方法只有...

在VS2017中用C#调用python脚本的实现

情景是这样的:在C#中调用python脚本进行post请求,python脚本中使用了requests包。 Python的开发环境我们有比较多的选择,pycharm、sublime tex...

python生成密码字典的方法

python生成密码字典的方法

这里我使用的是python27 主要用的是我之前博文里提到的itertools循环迭代的模块,用这个模块可以省不少事 首先要调用itertools import itertools...

python中的global关键字的使用方法

摘要 global 标志实际上是为了提示 python 解释器,表明被其修饰的变量是全局变量。这样解释器就可以从当前空间 (current scope) 中读写相应变量...