'error: "Cannot create an existing file" when it does not actually exist

I'm coding a sound recording and analysis program in python but I have a problem with the command (on first run it worked great but since IMPOSSIBLE):

os.mkdir(directory)

As output I get this:

An exception occurred: FileExistsError
[WinError 183] Could not create an already existing file: '\\output'

While in my code I check in the following way in case the folder is indeed already existing:

current_dir = os.getcwd()

# Define a "output" directory :
directory = "\\"+"output"

path = current_dir + directory

isExist = os.path.exists(path)      # Valeure booléenne


if not isExist:
    # Create the directory
    # 'result' in
    # current directory
    os.mkdir(directory)

I checked with the debugger and the "isExist" variable has the value "False" but when running the program tells me that the folder already exists ??? Weird... enter image description here

And of course the "output" folder does not appear in the file explorer! Completely illogical... enter image description here

Note that this code is in a GitHub repository so maybe the error comes from there but the repository does not contain an "output" folder either!

Thanks in advance to anyone who tries to help me.



Solution 1:[1]

Ok well I found the solution, in reality you have to replace "directory" by "path" in the command:

os.mkdir(path)

Because otherwise it will only work the first run and even if you manually delete the folder.

So here is the code if it can help someone else!

import os

current_dir = os.getcwd()

# Define a "output" directory :

directory = "\\"+"output"

path = current_dir + directory

isExist = os.path.exists(path)      # Valeure booléenne


if not isExist:
    # Create the directory
    # 'output' in
    # current directory
    os.mkdir(path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1