SUNDARAM SRIVASTAVA
SUNDARAM SRIVASTAVA

Reputation: 150

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. 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. 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:-

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')

Upvotes: 0

Views: 3416

Answers (2)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

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.

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')

Upvotes: 1

Niko Fohr
Niko Fohr

Reputation: 33770

You cannot use os.rename if the file is in use. You could use shutil.copy instead.

Upvotes: 0

Related Questions