import os import random path = 'sis-novel1/files' files = os.listdir(path) random.shuffle(files) files = files[:1000 ] #merge files to one txt with open('sel.txt', 'w', encoding='utf-8') as f: for i,file in enumerate(files): print(i,len(files)) with open(path + '/' + file, 'r', encoding='utf-8') as f1: text=f1.read() text=text.replace('\n\n','[dooubleenter]') text=text.replace('\n','') text=text.replace('[dooubleenter]','\n') f.write(text)