files.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ## This module implements file handling.
  2. ##
  3. ## **See also:**
  4. ## * `paths module <paths.html>`_ for path manipulation
  5. from std/paths import Path, ReadDirEffect, WriteDirEffect
  6. from std/private/osfiles import fileExists, removeFile,
  7. moveFile
  8. proc fileExists*(filename: Path): bool {.inline, tags: [ReadDirEffect], sideEffect.} =
  9. ## Returns true if `filename` exists and is a regular file or symlink.
  10. ##
  11. ## Directories, device files, named pipes and sockets return false.
  12. result = fileExists(filename.string)
  13. proc removeFile*(file: Path) {.inline, tags: [WriteDirEffect].} =
  14. ## Removes the `file`.
  15. ##
  16. ## If this fails, `OSError` is raised. This does not fail
  17. ## if the file never existed in the first place.
  18. ##
  19. ## On Windows, ignores the read-only attribute.
  20. ##
  21. ## See also:
  22. ## * `removeDir proc <dirs.html#removeDir>`_
  23. ## * `moveFile proc`_
  24. removeFile(file.string)
  25. proc moveFile*(source, dest: Path) {.inline,
  26. tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect].} =
  27. ## Moves a file from `source` to `dest`.
  28. ##
  29. ## Symlinks are not followed: if `source` is a symlink, it is itself moved,
  30. ## not its target.
  31. ##
  32. ## If this fails, `OSError` is raised.
  33. ## If `dest` already exists, it will be overwritten.
  34. ##
  35. ## Can be used to `rename files`:idx:.
  36. ##
  37. ## See also:
  38. ## * `moveDir proc <dirs.html#moveDir>`_
  39. ## * `removeFile proc`_
  40. moveFile(source.string, dest.string)