aTxt文件夹有3000个文本文件。我的代码以随机顺序读取这些.txt文件,如(1.txt,10.txt,1000.txt,...)。如何连续读取这些文本文件(比如首先读取1.txt然后读取2.txt,依次读取3000.txt)并将这些文本文件附加到数组中?
from pickle import dump
import glob
textlist = []
textfiles = glob.glob('D:/qrt/aTxt/*.txt')
for x in textfiles:
x1 = open(x, 'r')
x2 = x1.read()
textlist.append(x2)
x1.close()
dump(textlist, open('textlists.pkl', 'wb'))
答案 0 :(得分:1)
如果您的意思是词汇顺序,您只需使用内置函数glob
sorted()
获得的文件路径列表进行排序
from pickle import dump
import glob
textlist = []
textfiles = glob.glob('D:/qrt/aTxt/*.txt')
for filepath in sorted(textfiles):
with open(filepath , 'rt') as finput:
content = finput.read()
textlist.append(content)
dump(textlist, open('textlists.pkl', 'wb'))