Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Racket, how do I get or change my working directory?

What is Racket's equivalent for viewing and changing the working directory of a process like pwd and cd?

like image 399
Sage Gerard Avatar asked Jul 29 '18 13:07

Sage Gerard


People also ask

How do I find my current working directory?

To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .

How do I use current working directory?

To list the files in the current directory use the dir command, and if you want to change the current directory, use the cd command. You can use the chdir command by itself to print the current directory in MS-DOS and the Windows command line.


Video Answer


2 Answers

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
like image 112
Sage Gerard Avatar answered Oct 09 '22 03:10

Sage Gerard


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]

like image 22
calvin Avatar answered Oct 09 '22 05:10

calvin