Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a path exists?

Tags:

rust

The choice seems to be between std::fs::PathExt and std::fs::metadata, but the latter is suggested for the time being as it is more stable. Below is the code I have been working with as it is based off the docs:

use std::fs;

pub fn path_exists(path: &str) -> bool {
    let metadata = try!(fs::metadata(path));
    assert!(metadata.is_file());
}

However, for some odd reason let metadata = try!(fs::metadata(path)) still requires the function to return a Result<T,E> even though I simply want to return a boolean as shown from assert!(metadata.is_file()).

Even though there will probably be a lot of changes to this soon enough, how would I bypass the try!() issue?

Below is the relevant compiler error:

error[E0308]: mismatched types
 --> src/main.rs:4:20
  |
4 |     let metadata = try!(fs::metadata(path));
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^ expected bool, found enum `std::result::Result`
  |
  = note: expected type `bool`
             found type `std::result::Result<_, _>`
  = note: this error originates in a macro outside of the current crate

error[E0308]: mismatched types
 --> src/main.rs:3:40
  |
3 |   pub fn path_exists(path: &str) -> bool {
  |  ________________________________________^
4 | |     let metadata = try!(fs::metadata(path));
5 | |     assert!(metadata.is_file());
6 | | }
  | |_^ expected (), found bool
  |
  = note: expected type `()`
             found type `bool`
like image 645
Juxhin Avatar asked Sep 03 '15 20:09

Juxhin


People also ask

How do you check if a path exists or not in Python?

os. path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows a symbolic link, which means if the specified path is a symbolic link pointing to a directory then the method will return True.

How do you check path is exist or not in Java?

File. exists() is used to check whether a file or a directory exists or not. This method returns true if the file or directory specified by the abstract path name exists and false if it does not exist.

How do I find the current path of a file?

Run it with the python (or python3 ) command. You can get the absolute path of the current working directory with os. getcwd() and the path specified with the python3 command with __file__ . In Python 3.8 and earlier, the path specified by the python (or python3 ) command is stored in __file__ .


2 Answers

Note that many times you want to do something with the file, like read it. In those cases, it makes more sense to just try to open it and deal with the Result. This eliminates a race condition between "check to see if file exists" and "open file if it exists". If all you really care about is if it exists...

Rust 1.5+

Path::exists... exists:

use std::path::Path;

fn main() {
    println!("{}", Path::new("/etc/hosts").exists());
}

As mental points out, Path::exists simply calls fs::metadata for you:

pub fn exists(&self) -> bool {
    fs::metadata(self).is_ok()
}

Rust 1.0+

You can check if the fs::metadata method succeeds:

use std::fs;

pub fn path_exists(path: &str) -> bool {
    fs::metadata(path).is_ok()
}

fn main() {
    println!("{}", path_exists("/etc/hosts"));
}
like image 129
Shepmaster Avatar answered Oct 03 '22 06:10

Shepmaster


You can use std::path::Path::is_file:

use std::path::Path;

fn main() {
   let b = Path::new("file.txt").is_file();
   println!("{}", b);
}

https://doc.rust-lang.org/std/path/struct.Path.html#method.is_file

like image 39
Zombo Avatar answered Oct 03 '22 08:10

Zombo