What is the fucntion of mkdir -p in Python

0 votes
I am looking for a solution other than a system call, is there a way to get functionality similar to mkdir -p on the shell from within Python. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?
Oct 30, 2018 in Python by findingbugs
• 3,260 points
932 views

1 answer to this question.

0 votes

mkdir -p gives you an error if the file already exists:

$ touch /tmp/foo
$ mkdir -p /tmp/foo
mkdir: cannot create directory `/tmp/foo': File exists

You can try the full code as following

import os
def mkdirp(directory):
    if not os.path.isdir(directory):
        os.makedirs(directory) 
answered Oct 30, 2018 by Priyaj
• 58,020 points

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,885 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
1,261 views
+7 votes
8 answers

What exactly is the function of random.seed() in python?

The seed method is used to initialize the ...READ MORE

answered Oct 29, 2018 in Python by Rahul
126,841 views
0 votes
1 answer

What is the purpose of inner class in Python?

Advantages of inner class: Logical grouping of classes: ...READ MORE

answered Feb 7, 2019 in Python by SDeb
• 13,300 points
2,312 views
0 votes
1 answer

How to create new folder?

You can create a folder with os.makedirs() and use os.path.exists() to ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 58,020 points
890 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 58,020 points
6,474 views
0 votes
1 answer

What is the use of “assert” keyword in Python?

You can try the following in a ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,020 points
1,068 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