Home > Backend Development > Python Tutorial > python实现文件分组复制到不同目录的例子

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:30:33
Original
1331 people have browsed it

场景:某个文件夹下面包含数量巨大的文件,需求需要将这些文件按组(比如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')

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template