Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.mkdir(path) returns OSError when directory does not exist

I am calling os.mkdir to create a folder with a certain set of generated data. However, even though the path I specified has not been created, the os.mkdir(path) raises an OSError that the path already exists.

For example, I call:

os.mkdir(test) 

This call results in OSError: [Errno 17] File exists: 'test' even though I don't have a test directory or a file named test anywhere.

NOTE: the actual path name I use is not "test" but something more obscure that I'm sure is not named anywhere.

Help, please?

like image 614
Quanquan Liu Avatar asked Sep 24 '13 05:09

Quanquan Liu


People also ask

What does os mkdir return?

os. mkdir() creates a new directory (folder).

How do you create directory in Python if it does not exist?

import os path = '/Users/krunal/Desktop/code/database' os. makedirs(path, exist_ok=False) print("The new directory is created!") So that's how you easily create directories and subdirectories in Python with makedirs(). That's it for creating a directory if not exist in Python.

Does os mkdir create parent directory?

makedirs always automatically creates any intermediate parent directories that don't already exist, and the exist_ok=True argument tells makedirs not to raise an error if the /tmp/my/new/dir/ directory already exists.

How do you check whether a directory exists or not in Python?

os. path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows a symbolic link, which means if the specified path is a symbolic link pointing to a directory then the method will return True.


2 Answers

Greg's answer is correct but doesn't go far enough. OSError has sub-error conditions, and you don't want to suppress them all every time. It's prudent to trap just expected OS errors.

Do additional checking before you decide to suppress the exception, like this:

import errno import os  try:     os.mkdir(dirname) except OSError as exc:     if exc.errno != errno.EEXIST:         raise     pass 

You probably don't want to suppress errno.EACCES (Permission denied), errno.ENOSPC (No space left on device), errno.EROFS (Read-only file system) etc. Or maybe you do want to -- but that needs to be a conscious decision based on the specific logic of what you're building.

Greg's code suppresses all OS errors; that's unsafe just like except Exception is unsafe.

As others have pointed out, newer versions of Python provide os.makedirs() that attempts to create the dir only if it doesn't exist, equivalent to mkdir -p from a unix command line.

like image 149
Chris Johnson Avatar answered Oct 18 '22 01:10

Chris Johnson


In Python 3.2 and above, you can use:

os.makedirs(path, exist_ok=True)

to avoid getting an exception if the directory already exists. This will still raise an exception if path exists and is not a directory.

like image 42
1'' Avatar answered Oct 17 '22 23:10

1''