I am concatenating a number of variables and I want to save that string as a file path.
Is there a way it will automatically create all appropriate directories if they don't exist without having to check "if exists" on each one
For example.
"C:\" + a + "\" + b+ "\" + d + "\" + d + ".txt"
Using os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all.
CreateDirectory(String) Creates all directories and subdirectories in the specified path unless they already exist. CreateDirectory(String, UnixFileMode) Creates all directories and subdirectories in the specified path with the specified permissions unless they already exist.
Use new FileInfo(path).Directory.Create()
.
(This creates anything in the hierarchy that's required. If the directory already exists it does nothing.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With