0

I am running the following code :

import os, configparser

config = configparser.ConfigParser()   
config = config.read(r'C:\Users\ms\Desktop\3815_ticket\pconfig.txt')
portfolio_path=config['Data']['portfolio']

os.rename(portfolio_path,'bloop')

I obtain the following error :

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\ms\\Desktop\\\\data_lin\\portfolio.xlsx' -> 'bloop'

I read some old posts about this error, hence I modified it as follow :

with config.read(r'C:\Users\ms\Desktop\3815_ticket\pconfig.txt') as x :
    portfolio_path=x['Data']['portfolio']

os.rename(portfolio_path,'bloop')

But now I obtain

AttributeError: __enter__

I then tried to avoid a with block; I copied my file paths stored in pconfig.txt and then deleted the configparser object, but I still obtain the [WinError 32] error. Any help would be appreciated !

Maxime
  • 1

1 Answers1

0

You don't need the with statement, and it can't help you here. The error AttributeError: __enter__ occurs because you are trying to use the with statement on an object that doesn't have __enter__ and __exit__ methods. See here for details: Explaining Python's '__enter__' and '__exit__'

The PermissionError has nothing to do with config.read(). Instead, it happens because of the os.rename(portfolio_path,'bloop') statement. The error message even tells you what the problem is. Microsoft Windows thinks that the spreadsheet with the file name portfolio_path (i.e. C:\Users\ms\Desktop\data_lin\\portfolio.xlsx) is already open. The proper thing to do then is to check if that file is indeed already open. If it is open, then if you can, close it and rerun your Python script. If it isn't, then find out why MS Windows thinks it's open.

jjramsey
  • 1,131
  • 7
  • 17