我想在一個單一文件中排序和唯一30個不同大小的文件。
每個文件都包含一行,並以換行符分隔。這意味着該文件在每一行上都有簡單的文本。
這裏是我想嘗試:我如何使用python模擬從目錄中排序和唯一的文件?
lines_seen = set() # holds lines already seen
outfile = open('out.txt', "w")
for line in open('d:\\testing\\*', "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
文件夾名稱testing
,它包含30個不同的文件,而我試圖組合成文件out.txt
。輸出將是輸出文件每行中的排序和唯一文本。
嗯,我認爲這很容易,如果我寫d:\\testing\\*
它會讀取文件夾中的文件。但我得到了錯誤:
Traceback (most recent call last):
File "sort and unique.py", line 3, in <module>
for line in open('d:\\testing\\*', "r"):
OSError: [Errno 22] Invalid argument: 'd:\\testing\\*'
我想知道怎樣才能擺脫這種錯誤,並有效地處理我的所有文件到一個單一的輸出,無需任何不成功。
請注意:RAM爲8 GB,文件夾大小約爲10 GB。
明星語法需要shell擴展。 'glob'模塊可以爲你做到這一點。 – cel