I want to read files from a config folder at the directory where the executable is located. I do that using the following functions:
use std::env;
// add part of path to te path gotten from fn get_exe_path();
fn get_file_path(path_to_file: &str) -> PathBuf {
let final_path = match get_exe_path() {
Ok(mut path) => {
path.push(path_to_file);
path
}
Err(err) => panic!("Path does not exists"),
};
final_path
}
// Get path to current executable
fn get_exe_path() -> Result<PathBuf, io::Error> {
//std::env::current_exe()
env::current_exe()
}
In my case, get_exe_path()
will return C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe
.
With get_file_path("Config\test.txt")
, I want to append Config\test.txt
To the above path. Then I get the following path to the file: C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe\Config\test.txt
The problem is that std::env::current_exe()
will get the file name of the executable also and I do not need that. I only need the directory where it is located.
Question
The following the following function call should return C:\Users\User\Documents\Rust\Hangman\target\debug\Config\test.txt
:
let path = get_file_path("Config\\test.txt");
How can I get the path from the current directory without the executable name like above example? Are there any other ways to do this than using std::env::current_exe()
The pwd command displays the full, absolute path of the current, or working, directory.
You can use WinDir variable to get the current directory. The Environment. GetEnvironmentVariable method can be used for that. Here is the code snippet that gets the current directory using VB.NET.
Find the executable's path and its directory by using System.Reflection.Assembly.GetExecutingAssembly ().Location. This will return the path to the executable.
Using pwd and Get-Location cmdlet in PowerShell, you can get current folder path, get script directory path, print current directory. Set-Location cmdlet used in article, to set working directory.
I n this tutorial, we are going to see how to get the path of the current directory in Python. The current directory is nothing else than the folder from where your script is executed. To perform this task, we will use the “os” module in Python. It has a method called getcwd () which will return the current directory.
In the above PowerShell script, get current directory full path C:\Backup\01-Sept\ and assign it to variable $curDir. Second command returns parent directory path as C:\Backup using Split-Path cmdlet.
PathBuf::pop
is the mirror of PathBuf::push
:
Truncates
self
toself.parent
.Returns
false
and does nothing ifself.file_name
isNone
. Otherwise, returnstrue
.
In your case:
use std::env;
use std::io;
use std::path::PathBuf;
fn inner_main() -> io::Result<PathBuf> {
let mut dir = env::current_exe()?;
dir.pop();
dir.push("Config");
dir.push("test.txt");
Ok(dir)
}
fn main() {
let path = inner_main().expect("Couldn't");
println!("{}", path.display());
}
There's also the possibility of using Path::parent
:
Returns the
Path
without its final component, if there is one.Returns
None
if the path terminates in a root or prefix.
In your case:
fn inner_main() -> io::Result<PathBuf> {
let exe = env::current_exe()?;
let dir = exe.parent().expect("Executable must be in some directory");
let mut dir = dir.join("Config");
dir.push("test.txt");
Ok(dir)
}
See also:
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