Search code examples
pythonmkdirpython-os

Unable to create a directory with os.mkdir though it doesn't exist


I tried to create a directory with os.mkdir, but it raised FileExistsError.

>>> import os
>>> os.mkdir('test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileExistsError: [Errno 17] File exists: 'test'

The file test does exist, but it is a regular file instead of a directory.

  0 -rw-r--r--  1 sparkandshine  staff       0 Jan 31 17:09 test

How do I create a directory test/ in this case?


Solution

  • Operating systems do not allow files and directories with the same name. A directory is simply a special type of file.

    See the following for more information: https://unix.stackexchange.com/questions/22447/why-cant-i-have-a-folder-and-a-file-with-the-same-name