Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting the error: "FileExistsError: [WinError 183] Cannot create a file when that file already exists"?

Tags:

python

windows

Why am I getting error, if my script creates a folder? I am using Python on Windows 7. The error:

FileExistsError: [WinError 183] Cannot create a file when that file already exists: [path to the file or folder in question]

The problem is that file and folder were not there.

like image 964
Santa Claus Avatar asked Feb 02 '19 07:02

Santa Claus


People also ask

How do you fix Cannot create a file when that file already exists?

Press Windows key + R to open up a Run dialog box. Then, type ms-settings:windowsupdate and press Enter to open up the Windows Update tab inside the Settings app. Run dialog: ms-settings:windowsupdate. Inside the Windows Update screen, click on Check for updates and install every available pending update.

Was Error 183 Cannot create a file when that file already exists?

183: Cannot create a new file when that file already exists. This is a generic Windows Services error that is returned when a service cannot be successfully started. There are a variety of possible situations that can prevent the Laserfiche Server service from successfully starting.


3 Answers

I just encountered this same issue. This thread helped me solve the problem, but the following clarification might help someone:

For me, the misunderstanding came from shutil.copytree(source, destination, symlinks, ignore).

I read destination as being the place where my copied tree will go. In reality, it creates that location and then copies the tree there. So if your destination directory already exists, like mine did, you'll get the error.

Hope that helps someone.

like image 126
jnutttzzz Avatar answered Oct 30 '22 07:10

jnutttzzz


As the comments point out, the folder already exists. You seem to think that attempts to create a folder that already exists should simply do nothing. But that is not how Windows sees it.

To avoid the error message, check first to see if the folder exists. Before the code that creates the folder, do

if not os.path.exists("name of folder"):
like image 37
BoarGules Avatar answered Oct 30 '22 05:10

BoarGules


I just encountered a slightly more subtle version of this, this might help someone else.

I was creating a folder with:
os.makedirs(os.path.dirname(my_filename), exist_ok=True)

Which should create the folder, but not error if it already exists. I'd run this numerous times with no problem.

Ran it again, and got an error:

FileExistsError: [WinError 183] Cannot create a file when that file already exists: '\my\folder\path'

Eventually worked out that I had kept the output folder from a previous run for comparison, by renaming it to something else.
Which shouldn't have caused a problem, but I still had one of the files from that folder open in Notepad. So since there was still an active reference to the folder the OS was throwing an error.

Closed the file in Notepad an re-ran, worked fine.

like image 42
Mick O'Hea Avatar answered Oct 30 '22 05:10

Mick O'Hea