在Racket中,如何获取或更改工作目录?

时间:2018-07-29 13:29:57

标签: operating-system racket

对于查看和更改pwdcd之类的进程的工作目录,Racket等效于什么?

2 个答案:

答案 0 :(得分:3)

使用current-directory

不传递任何参数将返回当前工作目录。传递路径会将工作目录更改为该路径。

以下是REPL的示例,该示例打印当前目录,然后更改到父目录:

> (current-directory)
#<path:/home/sage/>
> (current-directory (build-path (current-directory) ".."))
; now in /home

答案 1 :(得分:0)

请注意,路径是球拍中的类型对象。而且由于Racket会进行惰性评估,因此在替换中工作时不会检查您的值。

如果您这样做:

> (current-directory "/somepath/thatdoesnt/exist/") ; now in /somepath/thatdoesnt/exist

球拍不会抛出错误。尝试对路径对象本身进行操作时,只会出现错误。

例如:

> (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]

相关问题