How to check if a file exists in Nim?
A simple question that might be too long to look for in the official documentation!
I wish someone would have asked that question here.
Asked
Active
Viewed 295 times
-2

Alexandre Daubricourt
- 3,323
- 1
- 34
- 33
-
you can use the search bar in any doc page, or ctrl-f on https://nim-lang.org/docs/theindex.html for, e.g. 'exists' – shirleyquirk Dec 28 '22 at 12:01
-
@shirleyquirk I'm aware of that feature, I would just like to have basic docs from Google like for any mainstream language :p – Alexandre Daubricourt Dec 28 '22 at 14:33
-
you and me both. how about: https://www.google.com/search?q=site%3Anim-lang.org+file+exists first result is an (outdated) forum post, second result is the right doc page. – shirleyquirk Dec 29 '22 at 10:16
-
@shirleyquirk Googling "how to check if file exists in nim" and finding a code snippet right away without searching/scrolling on the whole doc page is much more convenient! I think that's kindof the purpose of SO ahahah – Alexandre Daubricourt Dec 29 '22 at 11:58
-
1SO is to solve _real_ programming questions, they could overlap with what you say. But the Nim library docs have a nice embbeded search function, e.g. searching for "exist" you get: `existsDir`, `existsEnv`, `existsFile`, `existsOrCreateDir`, `existsCookie`, `symlinkExists`, and `fileExists` for FTP client. That search function is what I use the most when programming in Nim. – xbello Dec 30 '22 at 10:35
-
@xbello yup it guess it really depends on your workflow. For my part I try to close as many browser tabs as I can. So if I get any question about Nim I have to open a new tab, go to the official docs, and then search and hope what I'm looking for is contained into the function name, if not I might get some results related to what's in the description, but it will never be as good as Google. Just seems so much more straightforward to google something right into a new tab URL bar. TBH it's the same for mainstream programming languages like JS, people go more on SO than MDN for basic function use – Alexandre Daubricourt Dec 30 '22 at 10:58
-
SO is not made and will never be made to replicate software documentation. Software is ever-evolving. What if the API changes ? It is better practice to look the original documentation since it will always be more up to date. – Dimitri Lesnoff Jan 11 '23 at 13:58
1 Answers
4
import os
import std/strformat
if fileExists(path):
echo &"path {path} exists!"

Alexandre Daubricourt
- 3,323
- 1
- 34
- 33