python实现从文件夹随机拷贝出指定数量文件到目标文件夹

为了方便倒腾数据,功能如题,该脚本和操作目录在同一根目录

实际运行时要手动修改程序中:cpfile_rand('img', 'outfile', 10) # 操作目录,输出目录,输出数量

import os
import random
import shutil

def cpfile_rand(img, outfile, num):
    list_ = os.listdir(img)
    if num > len(list_):
        print('输出数量必须小于:', len(list_))
        exit()
    numlist = random.sample(range(0,len(list_)),num) # 生成随机数列表a
    cnt = 0
    for n in numlist:
        filename = list_[n]
        oldpath = os.path.join(img, filename)
        newpath = os.path.join(outfile, filename)
        shutil.copy(oldpath, newpath)
        print('剩余文件:', num-cnt)
        cnt = cnt + 1
    print('==========task OK!==========')
if __name__ == "__main__":
    cpfile_rand('img', 'outfile', 10) # 操作目录,输出目录,输出数量

  

猜你喜欢

转载自www.cnblogs.com/niulang/p/12034924.html
今日推荐