write multiple files at a time

前端 未结 5 487
长情又很酷
长情又很酷 2021-01-13 20:04

I have a file with 196 list in it,and I want to create new 196 output files and write each of the list in a new file, so that I will have 196 output files each containing 1

5条回答
  •  被撕碎了的回忆
    2021-01-13 20:12

    I think this is what you are looking for:

    with open("/home/vidula/Desktop/project/ori_tri/inpt.data","r") as fn:
        listLines = fn.readlines()
    
    for fileNumber, line in enumerate(listLines):
        with open("/home/vidula/Desktop/project/ori_tri/input{0}.data".format(fileNumber), "w") as fileOutput:
            fileOutput.write(line)
    

提交回复
热议问题