原博客迁移到:https://blog.csdn.net/u013171226/article/details/107680310

import os

def updateFile(file,str):
    """
    在文件的每一行前面增加关键字
    :param file:文件名
    :param str:关键字
    """
    file_data = ""
    with open(file, "r") as f:
        for line in f:
            line = str + line 
            file_data += line
    with open(file,"w") as f:
        f.write(file_data)

base_dir = "./labels"
str  = "head,"
for file_name in os.listdir(base_dir):
    file_name = os.path.join(base_dir, file_name)
    print(file_name)
    updateFile(file_name, str)

 

相关文章:

  • 2021-06-06
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-12-27
  • 2021-05-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案