Q&A

Questions &
Answers with MarsDevs

HTML & CSS
JavaScript

How do I create a directory, and any missing parent directories?

Solution:

If you want to do this programmatically, you can use Python. Just open a text editor and create a script, for example, create_directory.py:


import os

def create_directory(path):
    try: 
        os.makedirs(path)
        print(f"Directory '{path}' created successfully.")
    except OSError as e:
        print(f"Error creating directory '{path}': {e}")

#Example Usage
directory_path = "/path/to/your/directory"
create_directory(directory_path)

Save it and then run it on a Python interpreter.


python create_directory.py

Next, replace /path/to/your/directory with the path you want to create.

This Python script uses os.makedirs() to create the directory and any missing parent directories. Also, it catches any errors that might occur during the creation process.

Follow our blogs to learn more about such Python tricks!