Skip to content

Python – How to create a nested directory structure

Category
Python
Versions & support
About Python →

The pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems. We can create nested directory structure suing pathlib.Path.mkdir function in Python.

  • On Linux:
  • On Windows:

Here:

  • parents=True: instruct to create parent directories if not available.
  • exist_ok=True: Do not throw an error if a directory already exists.

Execute the above Python script and checked for the newly created directory tree.

/dir1
└── dir2
    └── dir3

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top