Hamed Minaee
Hamed Minaee

Reputation: 2560

os.makedirs does not create folder on windows

I am using python 3.7 and the following command to create a directory which works on linux but not on windows:

       try:
        #shutil.rmtree('../../dist')
        os.makedirs('../../dist')
    except OSError as e:
        print("fffffffffffffffffffffffffffff")
        print(e)
        if e.errno != errno.EEXIST:
            raise

Here is the error I get when I run it on windows:

fffffffffffffffffffffffffffff
[WinError 183] Cannot create a file when that file already exists: 
'../../dist'

and there is no dist folder at all and I have no idea what that error is

Any idea?

Upvotes: 0

Views: 2766

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155448

Comment as answer per OP's request:

The problem here is that you're providing a path relative to the script, but relative paths are interpreted relative to the process's working directory, which is often completely different from the script location. The directory already exists relative to the working directory, but you're looking at a path relative to the script, and (correctly) finding nothing there.

If the directory must be created relative to the script, change the code to:

scriptdir = os.path.dirname(__file__)
# abspath is just to simplify out the path so error messages are plainer
# while os.path.join ensures the path is constructed with OS preferred separators
newdir = os.path.abspath(os.path.join(scriptdir, '..', '..', 'dist'))
os.makedirs(newdir)

Upvotes: 1

Related Questions