我正在尝试打开一个文件,如果它不存在,请创建它。但是由于某种原因,我收到消息"Not a directory"
ErrorKind::Other
的错误。这是我得到的完整错误:
线程"main"对"打开文件时出现未知错误"感到恐慌 { 代码:20,种类:其他,消息:"不是目录" }", src/main.rs:53:17
这是我的代码:
let mut filepath = std::env::current_exe()?;
filepath.push("days");
filepath.set_extension("toml");
let mut file = match File::open(filepath.clone()) {
Ok(f) => f,
Err(e) => match e.kind() {
ErrorKind::NotFound => {
{
let mut create_file = File::create(filepath.clone());
create_file.write_all(...); // Dummy data
}
File::open(filepath.clone())?
},
_ => {
panic!("Unknow error when opening file: {:?}", e); // This is line 53
}
}
};
当我将文件路径打印到控制台进行检查时,我得到"/mnt/c/Projects/Other/random/hmdict/target/debug/hmdict/days.toml"
,这是正确的。
有谁知道为什么我收到操作系统错误 20"不是目录"?
附言我正在使用 Rustnightly-x86_64-unknown-linux-gnu
和 rustc 版本1.46.0-nightly
在 Windows 10 build 18363 版本 1909 上运行的 WSL 中
问题是推送会创建一个单独的路径。如果可执行文件位置<what_ever>/hmdict
,则代码会创建一个路径<whatever>/hmdict/days.toml
。显然,此路径不存在,因为hmdict
是可执行文件而不是目录。