firstly files is a 1 dimensional python list of the names of the files in "filePath/"
from what I can tell the problem lies in calculate_bigram as you are opening a file using read rather than write, therefore you will get an error when trying to write to it. I tried this:
def calculate_bigram(file):
if os.path.isfile(file):
with open(file, "w") as byte_file:
byte_file.write("this is a test")
import os
from multiprocessing import Pool
if __name__ == "__main__":
#Using multiprocessing to calculate bi-grams
files = os.listdir('files/')
path = os.path.dirname(__file__)
for idx, file in enumerate(files):
files[idx] = os.path.join(path, "files", file)
with Pool(processes=4) as pool:
pool.map(calculate_bigram, files)
and the files dir looks like this
files
|-> a.txt
|-> b.txt
|-> sub
|-> c.txt
additionaly you have to suply the full path not the path in relation to the file your executing hence the
path = os.path.dirname(__file__)
for idx, file in enumerate(files):
files[idx] = os.path.join(path, "files", file)
because pool changes the execution direcory so the files end up someware you dont want it
Edit: to your comment:you still have to specify the full path and not the path in relation to the current execution. at least that's how it works for me
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…