I am executing a file copy operation using python. I am executing os.makedirs(dest, exist_ok=True)
to make sure that the destination folder exists before copying the file, and create the folder if it doesn’t exist. I am getting the following error:
Traceback (most recent call last) :
File "pathtoscriptscript.py", line 93, in <module>
os.makedirs(dest, exist_ok=True)
File "<frozen os>", line 225, in makedirs
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'pathtofile'
Interesting thing is that, I am able to execute this code successfully from my user account, but the error is thrown when it is executed by a service account.
The previous Question-Answers suggest:
- use of ‘exist_ok’ paramater, I am passing
exist_ok=True
. - A comment suggests to close the open files from the folder and then execute the script. I have checked and confirmed that- no files from the folder are being used. Also, the script is being executed successfully through my user account, but showing the error when executed by the service account. So, I guess the issue should not be about open files.
- A comment suggests that ‘exist_ok’ parameter returns the error if a file with same name(the name of folder you are trying to create) already exists at the location. I have checked and confirmed that- there is no file with same name at the specified location.
I am not sure why this error is being encountered. I expect that the following change should solve the problem-
if not os.path.exists(dest):
os.path.makedirs(dest, exist_ok=True)
But, I was curious about the reason behind this and also the code-change would add another iteration of testing(which I want to avoid). I would like to know any workaround for the issue.
Thanks,
Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.