🗂 Rust OS/File System Utilities
Uses from:
📄 Check if a file or directory exists
🆕 Create a file (overwrite if exists)
Overwrites if file already exists.
➕ Append to a file (or create if doesn't exist)
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open("hello.txt")?;
file.write_all(b"\nAppended line")?;
📖 Read a file (whole content as String)
🔄 Read raw bytes from a file
🗑 Delete a file
📁 Create a directory
- Recursive:
fs::create_dir_all("a/b/c")?;
🗑 Delete a directory
- Recursive delete:
fs::remove_dir_all("a/b")?;
📜 List files in a directory
for entry in fs::read_dir("some_folder")? {
let entry = entry?;
println!("{:?} : {:?}", entry.path(), entry.file_type().unwrap().is_file());
}
✅ Summary Table
Task | Function / Method |
---|---|
Check if file/dir exists | Path::exists() |
Create (overwrite) file | File::create() |
Append to file | OpenOptions::new().append(true)... |
Read file as String |
fs::read_to_string() |
Read file as bytes | fs::read() |
Write bytes to file | file.write_all(b"...") |
Delete file | fs::remove_file() |
Create directory | fs::create_dir() / create_dir_all() |
Delete directory | fs::remove_dir() / remove_dir_all() |
List dir entries | fs::read_dir() |