Python修改txt某列元素值,图片重命名

两个问题:1.将txt文件第一列的0元素改为1, 1改为0

                  2.将所有图片重命名

1toot0.py

'''
     将txt文件第一列的0元素改为1, 1改为0
'''
import os
import numpy as np

def renamefile():
    # path = 'C:/Users/10974/Desktop/yanlabel/'
    path = 'C:/Users/10974/Desktop/open_firelabel/test/'
    try:
          filelist = os.listdir(path)
    except:
        print('找不到指定路径')
    for f in filelist:
        with open(path+f, 'r') as c:
            lines = c.readlines()
            allLine = []
            for line in lines:
                line = list(line)
                if line[0] == "0":
                    line[0] = "1"
                else:
                    line[0] = "0"
                allLine.append(line)
            data = sum(allLine, [])
            seq = "".join(data)
        with open(path+f, 'w') as c:
            c.writelines(seq)

if __name__=='__main__':
    renamefile()

picture_rename.py

import os
import numpy as np

def renamefile():
    # path=input("输入文件夹路径:
    # path = 'C:/Users/10974/Desktop/yan2/'
    path = 'C:/Users/10974/Desktop/YH/DATASET/open_fire/open_fire/'
    try:
          filelist=os.listdir(path)
    except:
        print('找不到指定路径')
        return
    # if path[-1:]=='/'  :
    #     path=path
    # else:
    #     path=path+'/'
    # n=0
    n=0
    for f in filelist:
        # 设置旧文件名(就是路径+文件名)
        # oldname = path + os.sep + filelist[n]  # os.sep添加系统分隔符
        # 设置新文件名
        # newname = path + os.sep + 's' + str(n + 1) + '.jpg'
        # print(oldname, '======>', newname)
        os.rename(path + str(f), path + 'fands' + str(n+1) + '.jpg')
        n += 1

if __name__=='__main__':
    renamefile()

你可能感兴趣的:(Python,目标检测,python)