python – multi folders with nested structure
Question:
how to create a multi folders with nested structure in python api in a simple way with out loops like unix command mkdir -p a/b/c/{d,e,f}
seems like pathlib
or os.mkdirs
has no direct provision for this.
tried in this way ‘pathlib.Path(dest).mkdir(0o755, parents=True, exist_ok=True’) but not working
Answers:
You can use the os library’s makedirs
function to recursively create the nested folders. The exist_ok argument is set to True to avoid raising an error if the folders already exist
import os
path = 'a/b/c/d'
os.makedirs(path, exist_ok=True)
Similarly, you can create multiple nested folders in the same way:
paths = ['a/b/c/d', 'a/b/c/e', 'a/b/c/f']
for path in paths:
os.makedirs(path, exist_ok=True)
how to create a multi folders with nested structure in python api in a simple way with out loops like unix command mkdir -p a/b/c/{d,e,f}
seems like pathlib
or os.mkdirs
has no direct provision for this.
tried in this way ‘pathlib.Path(dest).mkdir(0o755, parents=True, exist_ok=True’) but not working
You can use the os library’s makedirs
function to recursively create the nested folders. The exist_ok argument is set to True to avoid raising an error if the folders already exist
import os
path = 'a/b/c/d'
os.makedirs(path, exist_ok=True)
Similarly, you can create multiple nested folders in the same way:
paths = ['a/b/c/d', 'a/b/c/e', 'a/b/c/f']
for path in paths:
os.makedirs(path, exist_ok=True)