How to create new folder?

0 votes

I want to put output information of my program to a folder. if given folder does not exist, then the program should create a new folder with folder name as given in the program. Is this possible? If yes, please let me know how.

Suppose I have given folder path like "C:\Program Files\alex" and alex folder doesn't exist then program should create alex folder and should put output information in the alex folder.

Aug 29, 2018 in Python by bug_seeker
• 15,350 points
27 views

2 answers to this question.

0 votes

You can create a folder with os.makedirs()

and use os.path.exists() to see if it already exists:

newpath = r'C:\Program Files\arbitrary' if not os.path.exists(newpath): os.makedirs(newpath)

If you're trying to make an installer: Windows Installer does a lot of work for you.

answered Aug 29, 2018 by Priyaj
• 56,520 points
0 votes
import os
try: 
    os.makedirs(path)
except OSError:
    if not os.path.isdir(path):
        raise
answered Aug 31, 2018 by Omkar
• 67,480 points

Related Questions In Python

+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,420 points
93 views
0 votes
1 answer

How to add a new line in Python?

You can use '\n' for a next ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
91 views
0 votes
1 answer

How to create transaction in GnuCash in response to an email?

Here's a template, along with a couple ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 56,520 points
53 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29 in Python by Nymeria
• 3,520 points
67 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 views
+4 votes
6 answers
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
• 56,520 points
122 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 56,520 points
133 views