gpt4 book ai didi

python - 为什么 Python 导出的文件没有关闭?

转载 作者:行者123 更新时间:2023-12-01 07:09:48 24 4
gpt4 key购买 nike

从 Python 导出 csv 文件时,由于某种原因,它不会关闭(即使使用“with”语句),因为当我事后调用它时,出现以下错误:

PermissionError: [WinError 32] The process cannot access the file because it is being used 
by another process

我认为它必须是挂起的 close 函数,因为当我在 with 语句或 close() 语句后面打印时,它会被打印 (例如打印文件名)。有什么建议可以解决这个问题吗?(此外,当我尝试打开导出的 CSV 文件时,我收到一条只读消息,因为它已被另一个程序使用。只有当 Python 关闭时我才能正确访问它,这很烦人)

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)

最佳答案

代码正确。问题一定出在其他地方。

要么是另一个被遗忘的Python进程,要么是@CristiFati提到的一个开放编辑器。

最坏的情况是重新启动PC,重新登录后直接调用python脚本。该错误不应再出现。

关于python - 为什么 Python 导出的文件没有关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58278448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com