Wednesday, May 3, 2017

python: read all files in the directory and copy the text in one file

from os import listdir
from os.path import isfile, join
mypath ="./puzzles/p"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath,f))]
#text = []
for file in onlyfiles:
    with open(mypath + "/" + file,'r') as f:
        text = f.readlines()
        #text = [l for l in text if "ROW" in l]
        with open("out.txt","a") as f1:
            f1.writelines(text)

No comments:

Post a Comment