How can I create a nested directory

0 votes

How to check if the directory a file is going to be written to exists, and if not, create the directory using Python?

Here is what I tried:

import os

file_path = "/my/directory/filename.txt"
directory = os.path.dirname(file_path)

try:
    os.stat(directory)
except:
    os.mkdir(directory)       

f = file(filename)

Somehow, I missed os.path.exists (thanks kanja, Blair, and Douglas). This is what I have now:

def ensure_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)

Is there a flag for "open", that makes this happen automatically?

Nov 18, 2020 in Python by kartik
• 37,510 points
340 views

1 answer to this question.

0 votes

Hello,

Using try except and the right error code from errno module gets rid of the race condition and is cross-platform:

import os
import errno

def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

In other words, we try to create the directories, but if they already exist we ignore the error. On the other hand, any other error gets reported. For example, if you create dir 'a' beforehand and remove all permissions from it, you will get an OSError raised with errno.EACCES

answered Nov 18, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How can I safely create a nested directory?

Python 3.5+: import pathlib pathlib.Path('/my/directory').mkdir(parents=True, exist_ok=True) pathlib.Path.mkdir as used above ...READ MORE

answered Nov 19, 2020 in Python by Gitika
• 65,910 points
2,663 views
0 votes
2 answers

How can I safely create a nested directory?

No need to put filename.txt For directory check path ...READ MORE

answered Nov 25, 2020 in Python by Deepak Chaurasia
1,328 views
0 votes
1 answer

How can I safely create a nested directory?

On Python ≥ 3.5, use pathlib.Path.mkdir: from pathlib import ...READ MORE

answered Nov 25, 2020 in Python by Gitika
• 65,910 points
349 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
958 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
3,441 views
0 votes
1 answer

How can I get the domain name of my site within a Django template?

Hello kartik, The variation of the context processor ...READ MORE

answered Apr 23, 2020 in Python by Niroj
• 82,880 points
9,329 views
0 votes
1 answer

How can I find script's directory with Python?

Hii, You need to call os.path.realpath on __file__, so that when __file__ is ...READ MORE

answered May 11, 2020 in Python by Niroj
• 82,880 points
591 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP