Opening a file returns ErrorKind::Other 'Error: Os, code: 20, message: "Not a directory"'
Opening a file returns ErrorKind::Other 'Error: Os, code: 20, message: "Not a directory"'
我正在尝试打开一个文件,如果它不存在,请创建它。但出于某种原因,我收到 ErrorKind::Other
错误消息 "Not a directory"
。这是我得到的完整错误:
thread 'main' panicked at 'Unknown error when opening file: Os { code: 20, kind: Other, message: "Not a directory" }', 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"
,这是正确的。
有谁知道我为什么会收到 os 错误 20 "Not a directory"?
P.S 我在 WSL 运行 Windows 10 build 18363 版本 1909[=17= 中使用 Rust nightly-x86_64-unknown-linux-gnu
和 rustc 版本 1.46.0-nightly
]
问题是 push 创建了一个单独的路径。如果您的可执行文件位置是 <what_ever>/hmdict
,您的代码会创建一个路径 <whatever>/hmdict/days.toml
。显然,这个路径不存在,因为 hmdict
是可执行文件而不是目录。
我正在尝试打开一个文件,如果它不存在,请创建它。但出于某种原因,我收到 ErrorKind::Other
错误消息 "Not a directory"
。这是我得到的完整错误:
thread 'main' panicked at 'Unknown error when opening file: Os { code: 20, kind: Other, message: "Not a directory" }', 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"
,这是正确的。
有谁知道我为什么会收到 os 错误 20 "Not a directory"?
P.S 我在 WSL 运行 Windows 10 build 18363 版本 1909[=17= 中使用 Rust nightly-x86_64-unknown-linux-gnu
和 rustc 版本 1.46.0-nightly
]
问题是 push 创建了一个单独的路径。如果您的可执行文件位置是 <what_ever>/hmdict
,您的代码会创建一个路径 <whatever>/hmdict/days.toml
。显然,这个路径不存在,因为 hmdict
是可执行文件而不是目录。