简体   繁体   English

获取 Python 错误 -->PermissionError: [WinError 32] 进程无法访问该文件,因为它正被另一个进程使用

[英]Getting Python error -->PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

I'm trying to read a csv file from local path using python & then I process the records of csv file into json structure, finally printing them on console.我正在尝试使用 python 从本地路径读取 csv 文件,然后我将 csv 文件的记录处理到 Z466DEEC76ECDF5FCA6D338 结构中,最后在控制台上打印。 I have written the code within try & except block.I'm expecting that if any exception happens in the try block while reading the data from csv file, the except block should print that exception has occured & it should move the csv file from current location to folder called errored.我已经在 try & except 块中编写了代码。我希望在从 csv 文件中读取数据时,如果 try 块中发生任何异常,则 except 块应该打印该异常已经发生并且它应该从当前移动 csv 文件文件夹的位置称为错误。 But while testing via simulating the errored scenario,it is unable to move the csv in errored folder.Instead it throws error:- "PermissionError: [WinError 32] The process cannot access the file because it is being used by another process".Below is the code:-但是在通过模拟错误场景进行测试时,无法将 csv 移动到错误文件夹中。而是抛出错误:-“PermissionError: [WinError 32] The process cannot access the file because it is being used by another process”。下面是代码: -

try:
   global df
   df = pd.read_csv('CBD_BU_FULL.csv', encoding='UTF-8', dtype=str)
   df = df.assign(FILE_TYPE ='BU')
   data = df.to_json(orient = "records", lines=False).split('\n')
   print(data)

except:
   print("An exception occurred")    
   os.rename('CBD_BU_FULL.csv', '/Errored/CBD_BU_FULL.csv')

It's quite possible that pd.read_csv isn't properly closing the file since it is crashing mid read, I would try opening the file yourself so that in the except your own program is definitely closing the file and this may fix your issue. pd.read_csv很可能没有正确关闭文件,因为它在读取过程中崩溃,我会尝试自己打开文件,以便在您自己的程序中肯定会关闭文件,这可能会解决您的问题。

import traceback
import pandas as pd
try:
   with open('CBD_BU_FULL.csv', "r") as f:
       df = pd.read_csv(f, encoding='UTF-8', dtype=str)
   df = df.assign(FILE_TYPE ='BU')
   data = df.to_json(orient = "records", lines=False).split('\n')
   print(data)

except:
   traceback.print_exc(1)
   os.rename('CBD_BU_FULL.csv', '/Errored/CBD_BU_FULL.csv')

You cannot use os.rename if the file is in use.如果文件正在使用中,则不能使用os.rename You could useshutil.copy instead.您可以改用shutil.copy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决这个 python 错误 - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: - How to tackle this python error - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: Python PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:mp4 文件 - Python PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: mp4 file Python-PermissionError:[WinError 32]该进程无法访问文件,因为该文件正在被另一个进程使用: - Python - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: pycaret 给出错误:PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用 - pycaret giving error:PermissionError: [WinError 32] The process cannot access the file because it is being used by another process copy2 PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用: - copy2 PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 更改图像名称 - PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用: - Changing name of image - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: PermissionError:[WinError 32]进程无法访问该文件,因为它正由另一个进程使用: - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用 - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process Django PermissionError: [WinError 32] 进程无法访问该文件,因为它正被另一个进程使用 - Django PermissionError: [WinError 32] The process cannot access the file because it is being used by another process Python 日志模块,日志文件问题:PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用 - Python Logging Module, logfile issue: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM