Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»Python – How to create a nested directory structure

    Python – How to create a nested directory structure

    By RahulSeptember 21, 20221 Min Read

    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.

    Advertisement
    • On Linux:
      1
      2
      from pathlib import Path
      Path("/dir1/dir2/dir3").mkdir(parents=True, exist_ok=True)
    • On Windows:
      1
      2
      from pathlib import Path
      Path("C:\\dir1\\dir2\\dir3").mkdir(parents=True, exist_ok=True)

    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
    
    

    directory Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Configure Postfix to Use Gmail SMTP on Ubuntu & Debian

    Deploying Flask Application on Ubuntu (Apache+WSGI)

    OpenSSL: Working with SSL Certificates, Private Keys and CSRs

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.