Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a file and automatically create directories

Tags:

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" 
like image 260
leora Avatar asked Mar 19 '09 00:03

leora


People also ask

How do I automatically create a folder in Python?

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.

What is directory creation?

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.


1 Answers

Use new FileInfo(path).Directory.Create().

(This creates anything in the hierarchy that's required. If the directory already exists it does nothing.)

like image 93
Jon Skeet Avatar answered Dec 01 '22 21:12

Jon Skeet