如何修复此错误 "The process cannot access the file because it is being used by another process"?
How do I fix this error "The process cannot access the file because it is being used by another process"?
这是引发错误的代码片段:
writer=pd.ExcelWriter('C:\Users\aji/Curve.xlsx',engine='openpyxl')
if os.path.exists('C:\Users\aji/Curve.xlsx'):
os.remove('C:\Users\aji/Curve.xlsx')
我收到此错误消息:
PermissionError: [WinError 32] The process cannot access the file because it is being used by
another process: 'C:\Users\aji/Curve.xlsx'
我很确定路径中的文件没有打开。是什么导致了这个问题,我该如何解决?
我认为您没有正确写入文件。结果,你的作者打开了文件。
The writer should be used as a context manager. Otherwise, call close()
to save and close any opened file handles.
试试这个(假设您要写入的 DataFrame 存储在 df
中):
with pd.ExcelWriter('C:\Users\aji/Curve.xlsx', engine='openpyxl') as writer:
df.to_excel(writer)
if os.path.exists('C:\Users\aji/Curve.xlsx'):
os.remove('C:\Users\aji/Curve.xlsx')
我在上面提供的 link 中还有其他很好的示例。我建议查看它们,以防另一个更适合您的用例。
正如一位评论者所建议的那样,混用斜杠令人困惑。要么到处使用反斜杠,要么到处使用正斜杠。但这在技术上不应该引起问题,它只是分散注意力。
这是引发错误的代码片段:
writer=pd.ExcelWriter('C:\Users\aji/Curve.xlsx',engine='openpyxl')
if os.path.exists('C:\Users\aji/Curve.xlsx'):
os.remove('C:\Users\aji/Curve.xlsx')
我收到此错误消息:
PermissionError: [WinError 32] The process cannot access the file because it is being used by
another process: 'C:\Users\aji/Curve.xlsx'
我很确定路径中的文件没有打开。是什么导致了这个问题,我该如何解决?
我认为您没有正确写入文件。结果,你的作者打开了文件。
The writer should be used as a context manager. Otherwise, call
close()
to save and close any opened file handles.
试试这个(假设您要写入的 DataFrame 存储在 df
中):
with pd.ExcelWriter('C:\Users\aji/Curve.xlsx', engine='openpyxl') as writer:
df.to_excel(writer)
if os.path.exists('C:\Users\aji/Curve.xlsx'):
os.remove('C:\Users\aji/Curve.xlsx')
我在上面提供的 link 中还有其他很好的示例。我建议查看它们,以防另一个更适合您的用例。
正如一位评论者所建议的那样,混用斜杠令人困惑。要么到处使用反斜杠,要么到处使用正斜杠。但这在技术上不应该引起问题,它只是分散注意力。