files.nim 1.3 KB

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