When exporting a csv-file from Python, for some reason it does not close (even when using the 'with' statement) because when I'm calling it afterwards I get the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process I suppose it has to be the close function that hangs, because when I'm printing behind the with statement or the close() statement, it gets printed (e.g. print fileName). Any suggestions that might solve this matter? (Also when I'm trying to open the exported CSV-file, I get a read-only message because it's used by another program. I can access it properly only when Python is closed, which is just annoying)
import csv, numpy, os import DyMat import subprocess os.chdir("C:/Users/myvhove/Documents/ResultsPyDymInt/Dymola/CoupledClutches") dm = DyMat.DyMatFile("dymatresfile") print(dm.names()) varList = ('J1.w', 'J2.w', 'J3.w', 'J4.w') fileName = dm.fileName + '.csv' with open(fileName, 'w', newline='') as oFile: csvWriter = csv.writer(oFile) vDict = dm.sortByBlocks(varList) for vList in vDict.values(): vData = dm.getVarArray(vList) vList.insert(0, dm._absc[0]) csvWriter.writerow(vList) csvWriter.writerows(numpy.transpose(vData)) subprocess.call("dymatresfile.csv") print(fileName)