What is Racket's equivalent for viewing and changing the working directory of a process like pwd
and cd
?
To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With