DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Path.mkdir(parents, exist_ok) in Python

Buy Me a Coffee

Path.mkdir() can create zero or more directories as shown below:

*Memos:

  • There is the 1st argument for Path() (Required-Type:str, bytes or os.PathLike). *Its argument name doesn't exist.
  • The 1st argument is mode for mkdir() (Optional-Default:0o777-Type:int).
  • The 2nd argument is parents for mkdir() (Optional-Default:False-Type:bool): *Memos:
    • If it's False, the path with a target directory and one or more non-existent parent directories cannot be created, getting FileNotFoundError.
    • If it's True, the path with a target directory and one or more non-existent parent directories can be created, not getting FileNotFoundError.
    • Basically, True is set to it.
  • The 3rd argument is exist_ok for mkdir() (Optional-Default:False-Type:bool): *Memos:
    • If it's False, FileExistsError is raised for an existent path.
    • If it's True, FileExistsError isn't raised for an existent path.
    • Basically, True is set to it.
#     Parent directories
#         ↓↓↓↓ ↓↓↓↓↓↓
p = Path('dir3/dir3_1/dir3_1_1')
#                     ↑↑↑↑↑↑↑↑
#                 A target directory
Enter fullscreen mode Exit fullscreen mode

1. Creating dir1 is successful:

from pathlib import Path

p = Path('dir1')

p.mkdir()
# Or
# p.mkdir(mode=0o777, parents=False, exist_ok=False)

# my_project
#  └-dir1 <- Here
Enter fullscreen mode Exit fullscreen mode

2. Recreateing dir1 is failed and gets FileExistsError:

from pathlib import Path

p = Path('dir1')

p.mkdir()
# FileExistsError: [Errno 17] File exists: 'dir1'

# my_project
#  └-dir1
Enter fullscreen mode Exit fullscreen mode

3. Recreating dir1 with exist_ok=True is failed but doesn't get FileExistsError:

from pathlib import Path

p = Path('dir1')

p.mkdir(exist_ok=True)

# my_project
#  └-dir1
Enter fullscreen mode Exit fullscreen mode

4. Creating dir1_1 in dir1 is successful:

from pathlib import Path

p = Path('dir1/dir1_1')

p.mkdir()

# my_project
#  └-dir1
#     └-dir1_1 <- Here
Enter fullscreen mode Exit fullscreen mode

5. Creating dir2/dir2_1 is failed and gets FileNotFoundError:

from pathlib import Path

p = Path('dir2/dir2_1')

p.mkdir()
# FileNotFoundError: [Errno 2] No such file or directory: 'dir2/dir2_1'

# my_project
#  └-dir1
#     └-dir1_1
Enter fullscreen mode Exit fullscreen mode

6. Creating dir2/dir2_1 with parents=True is successful:

from pathlib import Path

p = Path('dir2/dir2_1')

p.mkdir(parents=True)

# my_project
#  |-dir1
#  |  └-dir1_1
#  └-dir2 <- Here
#     └-dir2_1 <- Here
Enter fullscreen mode Exit fullscreen mode

7. Creating dir3/dir3_1/dir3_1_1 with parents=True and exist_ok=True is successful:

from pathlib import Path

p = Path('dir3/dir3_1/dir3_1_1')

p.mkdir(parents=True, exist_ok=True)

# my_project
#  |-dir1
#  |  └-dir1_1
#  |-dir2
#  |  └-dir2_1
#  └-dir3 <- Here
#     └-dir3_1 <- Here
#        └-dir3_1_1 <- Here
Enter fullscreen mode Exit fullscreen mode

8. Recreating dir3/dir3_1/dir3_1_1 with parents=True and exist_ok=True is failed but doesn't get FileExistsError:

from pathlib import Path

p = Path('dir3/dir3_1/dir3_1_1')

p.mkdir(parents=True, exist_ok=True)

# my_project
#  |-dir1
#  |  └-dir1_1
#  |-dir2
#  |  └-dir2_1
#  └-dir3
#     └-dir3_1
#        └-dir3_1_1
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay