1

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) 
1
  • You have another process (could it be an editor? or most likely another Python instance that failed, but it's still running), that's keeping the file open. You need to close / kill that 1st, before going on. Commented Oct 8, 2019 at 0:17

1 Answer 1

1

The code is correct. The problem must be somewhere else.

Either another forgotten python process or as @CristiFati mentioned an open editor.

In the worst case restart the PC and call the python script directly after logging in again. The error should no more be there.

Sign up to request clarification or add additional context in comments.

1 Comment

You were right. When trying to restart (with everything closed), my pc indicated something was still running in the background and nothing showed up in the Task Manager. Very weird. After restarting, the code working fine. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.