场景:某个文件夹下面包含数量巨大的文件,需求需要将这些文件按组(比如5000个一组)存放到不同的目录中去。
代码如下:
# filename: copyfiles.pyimport osimport 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’)