ITPub博客

首页 > IT职业 > IT生活 > 关于对bc系统搭建制作文件夹下文件的操作

关于对bc系统搭建制作文件夹下文件的操作

IT生活 作者:丝毫不给面子 时间:2019-09-11 17:32:50 0 删除 编辑
  1. 将文件夹下图片(文件)改名为1.jpg, 2.jpg, 3.jpg…
    bc系统搭建制作q<115.28.8.00.9.9>

  2. #coding=utf-8
    #函数功能:将指定文件夹下所有文件遍历,并修改文件名字
    import os

    fpath ="E:/ID_card/NewID/Dim-FaceID"#这里是你的第一级文件夹的路径

    def filechanger(path):
        filenames = os.listdir(path)#将path路径下的所有文件名存入列表filenames
        n = 1
        for file in filenames:#循环遍历每个文件
            filedomain = os.path.abspath(path)
            #file1 = os.listdir(filedomain)
            #path1 = path + "/" + file
            #print(path1)
            oldname = filedomain + os.sep + file
            #print(oldname)
            newname = filedomain + os.sep + str(n) + ".jpg"
            #print(newname)
            os.rename(oldname, newname)
            n += 1
            
    filechanger(fpath)


    其中值得注意的是用此方法获取的文件,并不是按顺序的。

    filenames = os.listdir(path)
    for file in filenames:

    2. 随机copy文件夹下n张图片入指定文件夹

    import os
    import random
    import shutil

    def copyFile(fileDir):
        #copy后存入的文件夹
        tarDir = "E:/ID_card/TEST/Test-FrontID/"
        
        pathDir = os.listdir(fileDir)
        # 随机200个文件
        sample = random.sample(pathDir, 200)
        #print (sample)

        for name in sample:
            #name1 = name.replace('jpg', 'xml')
            #print(name)
            #print(name1)
            #print(fileDir1 + name1)
            #print(fileDir + name)
            #print(tarDir + name)
            shutil.copyfile(fileDir + name, tarDir + name)
            #shutil.copyfile(fileDir1 + name1, tarDir1 + name1)
            #os.remove(fileDir + name)
            #os.remove(fileDir1 + name1)

    if __name__ == '__main__':
        #原文件存放的文件夹
        fileDir = "E:/ID_card/TEST/Total-FrontID/"
        
        #fileDir1 = "E:/ID_card/TEST/REC-Front/"
        #tarDir1 = "E:/ID_card/TEST/Test-FrontID1/"
        
        copyFile(fileDir)

    shutil.copyfile(A, B)是将A复制到B中去,A与B均为文件路径


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69948217/viewspace-2656797/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2019-09-11

  • 博文量
    1
  • 访问量
    405