write multiple files at a time

前端 未结 5 535
长情又很酷
长情又很酷 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:37

    I am assuming that you want to read 196 files and then write the data (after some modification) to new 196 files. If you use maps and reduce (functional programming) it can do what you want. Though without much explanation in the question, I am unable to help much.

    def modify(someString):
        pass # do processing
    
    def newfiles(oldfilename): return '%s.new.txt'%(oldfilename) # or something 
    
    filenames = ('a', 'b', 'c', 'd', ....) 
    handles = [(open(x, 'r'), open(newfile(x), 'w')) for x in filenames] # not using generator
    tmp = [y[1].write(modify(y[0].read())) for y in handles) 
    

提交回复
热议问题