Python 拆分文件夹中的文件 进行转移

import os
import shutil


def separate(src_path, target_path):
    files = os.listdir(src_path)
    s = []
    for file in files:  # 遍历文件夹
        if not os.path.isdir(file):  # 判断是否是文件夹,不是文件夹才打开
            #f = open(src_path + "/" + file);  # 打开文件
            # #iter_f = iter(f);  # 创建迭代器
            # str = ""
            # for line in iter_f:  # 遍历文件,一行行遍历,读取文本
            #     str = str + line
            if file[-3:]=='jpg':
                str = file
                s.append(str)  # 每个文件的文本存到list中
    print(s)  # 打印结果
    print(len(s))

    for file in files:
        if file[-2:]=='pt':
            str = file[:-2]+'jpg'
            s.remove(str)
            #shutil.move(src_path+'/'+str, target_path + '/' + str)
    for ele in s:
        shutil.move(src_path + '/' + ele, target_path + '/' + ele)
    print(s)
    print(len(s))
    return


if __name__ == "__main__":
    src_path = './TEST/source1_100'
    suffix = '.jpg .jpeg .png'
    #outfile = "./byf/list_dir.txt"
    #file = open(outfile,'w')
    target_path = './TEST/test_s100'
    separate(src_path,target_path)
    #file.close()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值