Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a given path is a file or directory?

I'm building a program that should be able to take both paths to files (*.*), and directories (./, ..). I want to be able to check if the path supplied is a file, or a directory.

like image 817
XAMPPRocky Avatar asked May 18 '15 17:05

XAMPPRocky


People also ask

How do you tell if a file is a directory?

The isDir() function is used to check a given file is a directory or not.

How do you check if path is file or directory JS?

The stats. isFile() method returns true if the file path is File, otherwise returns false. The stats. isDirectory() method returns true if file path is Directory, otherwise returns false.

How do you check if it is a file or directory in Python?

We use the is_file() function, which is part of the Path class from the pathlib module, or exists() function, which is part of the os. path module, in order to check if a file exists or not in Python.


1 Answers

You should use std::fs::metadata:

use std::fs::metadata;

fn main() {
    let md = metadata(".").unwrap();
    println!("is dir: {}", md.is_dir());
    println!("is file: {}", md.is_file());
}

Output:

is dir: true
is file: false
like image 152
fjh Avatar answered Sep 28 '22 05:09

fjh