Given a relative path:
PathBuf::from("./cargo_home")
Is there a way to get the absolute path?
A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path.
Use abspath() to Get the Absolute Path in Python To get the absolute path using this module, call path. abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.
Pass a string to File. expand_path to generate the path to that file or directory. Relative paths will reference your current working directory, and paths prepended with ~ will use the owner's home directory.
Rust 1.5.0 added std::fs::canonicalize
, which sounds pretty close to what you want:
Returns the canonical form of a path with all intermediate components normalized and symbolic links resolved.
Note that, unlike the accepted answer, this removes the ./
from the returned path.
A simple example from my machine:
use std::fs; use std::path::PathBuf; fn main() { let srcdir = PathBuf::from("./src"); println!("{:?}", fs::canonicalize(&srcdir)); let solardir = PathBuf::from("./../solarized/."); println!("{:?}", fs::canonicalize(&solardir)); }
Ok("/Users/alexwlchan/Developer/so-example/src") Ok("/Users/alexwlchan/Developer/solarized")
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