Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a directory exists and create a new one if it doesn't in Rust?

Tags:

I tried the following but I don't think it's particularly pretty:

let path = "target/dir"; if !std::path::Path::new(&path).exists() {     std::fs::create_dir(path)?; } 
like image 983
tversteeg Avatar asked Jan 01 '18 22:01

tversteeg


People also ask

How do you check if a folder exists and if not create it?

You can use os. path. exists('<folder_path>') to check folder exists or not.

How do you check if a directory exists in python and create it?

To create a directory if not exist in Python, check if it already exists using the os. path. exists() method, and then you can create it using the os. makedirs() method.

How do you check if a folder exists or not?

$Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.


1 Answers

std::fs::create_dir_all:

Recursively create a directory and all of its parent components if they are missing.

Examples

use std::fs;  fs::create_dir_all("/some/dir")?; 
like image 66
Shepmaster Avatar answered Oct 04 '22 23:10

Shepmaster