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.
The isDir() function is used to check a given file is a directory or not.
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.
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.
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
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