Search code examples
pythonmkdir

How to create new folder?


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.


Solution

  • 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.