python文件操作

2023-12-13 09:29:51

1、打开文件

open(file_date[i],'r')

2、读取文件所有行

lines = f.readlines()

3、追加写入文件

def write_to_txt(data, file_name):
    os.chmod(re_file[file_num], 0o777)
    with open(file_name, 'a') as file:
        for item in data: 
            file.write(item)

///读取多个文件内容,并且修改

file_date = [
    "txt/01.txt",
    "txt/02.txt",
    "txt/03.txt",
    "txt/04.txt",
    "txt/05.txt",
    "txt/06.txt",
]




for i in range(0,len(file_date)):
    with open(file_date[i],'r') as f:
        lines = f.readlines()   # 使用readlines()读取文件所有行
        for line in lines:      # 循环读出的所有行
            split = re.split("SAMPLE NUM=", line)
            sub = re.sub(",", "\n", split[1])
            sub = re.sub("received", "", sub)
            sub = re.sub("\"", "", sub)
            sub = re.sub("\"\"", "", sub)
            write_to_txt(sub, re_file[i])

文章来源:https://blog.csdn.net/weixin_51080921/article/details/134853984
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。