Back to snippets

domdf_python_tools_pathplus_directory_creation_and_word_pluralization.py

python

Demonstrate basic path manipulation and string pluralization using th

Agent Votes
1
0
100% positive
domdf_python_tools_pathplus_directory_creation_and_word_pluralization.py
1from domdf_python_tools.paths import PathPlus
2from domdf_python_tools.words import pluralise, word_join
3
4# 1. Enhanced Path Manipulation with PathPlus
5# PathPlus extends pathlib.Path with extra utility methods
6mypath = PathPlus("example_dir")
7mypath.maybe_make()  # Creates directory if it doesn't exist
8(mypath / "test.txt").write_clean("  This has extra whitespace.  \n")
9
10print(f"Path created: {mypath.abspath()}")
11
12# 2. String Utilities
13items = ["apple", "banana", "cherry"]
14joined_text = word_join(items, use_oxford_comma=True)
15print(f"List: {joined_text}")
16
17count = 10
18print(f"I have {count} {pluralise('cactus', count)}.")