How to read and write multiple files?

后端 未结 8 1153
南方客
南方客 2020-12-24 00:52

I want to write a program for this: In a folder I have n number of files; first read one file and perform some operation then store result in a separate file. Then

8条回答
  •  萌比男神i
    2020-12-24 01:10

    I've just learned of the os.walk() command recently, and it may help you here. It allows you to walk down a directory tree structure.

    import os
    OUTPUT_DIR = 'C:\\RESULTS'
    for path, dirs, files in os.walk('.'):
        for file in files:
            read_f = open(os.join(path,file),'r')
            write_f = open(os.path.join(OUTPUT_DIR,file))
    
            # Do stuff
    

提交回复
热议问题