In Racket, how do I get or change my working directory?
Use current-directory
.
Passing no arguments returns the current working directory. Passing a path changes the working directory to that path.
Here's an example for the REPL that prints the current directory, then changes to the parent directory:
> (current-directory)
#<path:/home/sage/>
> (current-directory (build-path (current-directory) ".."))
; now in /home
Note that path is a type-object in racket. And because in Racket current-directory
does not actually change the environment path but only changes the current-directory path value, if you do this:
> (current-directory "/somepath/thatdoesnt/exist/") ; now in /somepath/thatdoesnt/exist
Racket will not throw an error. You'll only get an error when you try to do something with the path object itself.
such as:
> (directory-list (current-directory)) ; directory-list: could not open directory ; path: /somepath/thatdoesnt/exist/ ; system error: No such file or directory; errno=2 ; [,bt for context]