這個想法是使用N
進程編寫N
文件。產生多個進程來編寫不同的文件Python
要寫入的對文件數據從存儲在具有列表作爲值的字典多個文件來了,它看起來像這樣:
dic = {'file1':['data11.txt', 'data12.txt', ..., 'data1M.txt'],
'file2':['data21.txt', 'data22.txt', ..., 'data2M.txt'],
...
'fileN':['dataN1.txt', 'dataN2.txt', ..., 'dataNM.txt']}
所以file1
是data11 + data12 + ... + data1M
等。 。
所以我的代碼如下所示:
jobs = []
for d in dic:
outfile = str(d)+"_merged.txt"
with open(outfile, 'w') as out:
p = multiprocessing.Process(target = merger.merger, args=(dic[d], name, out))
jobs.append(p)
p.start()
out.close()
和merger.py看起來是這樣的:
def merger(files, name, outfile):
time.sleep(2)
sys.stdout.write("Merging %n...\n" % name)
# the reason for this step is that all the different files have a header
# but I only need the header from the first file.
with open(files[0], 'r') as infile:
for line in infile:
print "writing to outfile: ", name, line
outfile.write(line)
for f in files[1:]:
with open(f, 'r') as infile:
next(infile) # skip first line
for line in infile:
outfile.write(line)
sys.stdout.write("Done with: %s\n" % name)
我看到文件夾上應該寫的文件,但它是空的。沒有頭,沒有什麼。我把印刷品放在那裏看是否一切正常,但沒有任何效果。
幫助!
你叫'out.close()'後立即'p.start()'。我懷疑合併任務是否有時間在文件被關閉之前執行。 – Blorgbeard
@Blorgbeard好點,但仍然沒有... – Pavlos
這是一個類似於操作系統的Linux,對吧? – tdelaney