首页 > 解决方案 > 如何从单个文件中读取特定行并在新文件的同一行中写入?

问题描述

import os
post_analysis_log = open("Complete_post_analysis.text",'w')

directory='/media/quinn/Joker/Anaysis/KCATBH00'

for x in range (1,6):
    os.chdir(directory +str(x)+'/')
    print(os.getcwd())
    with open ('post_analysis.log','r') as post:
        lines=post.readlines()

post_analysis_log.write("KCATBH00")
post_analysis_log.write(str(x))
post_analysis_log.write("    ")
post_analysis_log.write(lines[1])
post_analysis_log.write("    ")
post_analysis_log.write(lines[3])

这是我用它写在一行的代码,但不幸的是它写在另一行。

标签: pythonpython-3.xfile-handling

解决方案


推荐阅读