如何删除Racket中的非空目录?
当我调用此(delete-directory dir)
dir
为"/Users/apple/Library/Application Support/aoi"
时,我被告知:
exn found:#(struct:exn:fail:filesystem:errno delete-directory: cannot delete directory
path: /Users/apple/Library/Application Support/aoi
system error: Directory not empty; errno=66 #<continuation-mark-set> (66 . posix))
对于上下文,这是我的其余代码:
(define lib-folder '("/Users/apple/Library/Application Support/aoi"))
(define (del-lib-folder)
(for-each (λ (p)
(printf "del lib folder:~a~n" p)
(if (directory-exists? p)
(delete-directory p)
(printf "folder does not exist:~a~n" p)))
lib-folder))
我错过了什么吗?
答案 0 :(得分:3)
与许多操作系统一样,删除目录的命令仅在目录为空时才有效。例如。在GNU
# rmdir dir
rmdir: failed to remove 'dir/': Directory not empty
通常需要递归删除:
# rm -r dir
#
在#lang racket
中,您具有递归删除的函数delete-directory/files
。只需将delete-directory
替换为delete-directory/files
就可以实现您的目标:
(define lib-folder '("/Users/apple/Library/Application Support/aoi"))
(define (del-lib-folder)
(for-each (λ (p)
(printf "del lib folder:~a~n" p)
(if (directory-exists? p)
(delete-directory/files p)
(printf "folder does not exist:~a~n" p)))
lib-folder))
要非常小心,因为文件列表中的错误将完全按照您的要求执行,您可能永远不会收到这些文件。
答案 1 :(得分:0)
使用delete-directory / files:
(define lib-folder '("/Users/apple/Library/Application Support/aoi"))
(define (del-lib-folder)
(for-each (λ (p)
(printf "del lib folder:~a~n" p)
(delete-directory/files #:must-exist? #f p))
lib-folder))