如何使用Dir类删除非空目录?

时间:2022-03-10 22:46:10
Dir.delete("/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh")  

causes this error:

导致这个错误:

Directory not empty - /usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh

目录不是空的- /usr/local/var/ trisul/context0 / meters/oper/slice.9stmxh

How to delete a directory even when it still contains files?

如何删除一个目录,即使它仍然包含文件?

3 个解决方案

#1


47  

Is not possible with Dir (except iterating through the directories yourself or using Dir.glob and deleting everything).

不可能使用Dir(除非您自己遍历目录或使用Dir)。水珠和删除一切)。

You should use

你应该使用

require 'fileutils'
FileUtils.rm_r "/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh"

#2


6  

When you delete a directory with the Dir.delete, it will also search the subdirectories for files.

当你用删除目录删除一个目录时,它也会在子目录中搜索文件。

Dir.delete("/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh")

If the directory was not empty, it will raise Directory not empty error. For that ruby have FiltUtils.rm_r method which will delete the directory no matter what!

如果目录不是空的,它会引发目录不是空的错误。因为ruby有FiltUtils。rm_r方法将删除目录,无论什么!

require 'fileutils'
FileUtils.rm_r "/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh"

#3


0  

I use bash directly with the system(*args) command like this:

我使用bash直接使用系统(*args)命令如下:

folder = "~/Downloads/remove/this/non/empty/folder"
system("rm -r #{folder}")

It is not really ruby specific but since bash is simpler in this case I use this frequently to cleanup temporary folders and files. The rm command just removes anything you give it and the -r flag tells it to remove files recursively in case the folder is not empty.

它并不是ruby特有的,但是由于bash在这种情况下更简单,所以我经常使用它来清理临时文件夹和文件。rm命令只删除您给它的任何内容,而-r标志告诉它递归地删除文件,以防文件夹不是空的。

#1


47  

Is not possible with Dir (except iterating through the directories yourself or using Dir.glob and deleting everything).

不可能使用Dir(除非您自己遍历目录或使用Dir)。水珠和删除一切)。

You should use

你应该使用

require 'fileutils'
FileUtils.rm_r "/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh"

#2


6  

When you delete a directory with the Dir.delete, it will also search the subdirectories for files.

当你用删除目录删除一个目录时,它也会在子目录中搜索文件。

Dir.delete("/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh")

If the directory was not empty, it will raise Directory not empty error. For that ruby have FiltUtils.rm_r method which will delete the directory no matter what!

如果目录不是空的,它会引发目录不是空的错误。因为ruby有FiltUtils。rm_r方法将删除目录,无论什么!

require 'fileutils'
FileUtils.rm_r "/usr/local/var/lib/trisul/CONTEXT0/meters/oper/SLICE.9stMxh"

#3


0  

I use bash directly with the system(*args) command like this:

我使用bash直接使用系统(*args)命令如下:

folder = "~/Downloads/remove/this/non/empty/folder"
system("rm -r #{folder}")

It is not really ruby specific but since bash is simpler in this case I use this frequently to cleanup temporary folders and files. The rm command just removes anything you give it and the -r flag tells it to remove files recursively in case the folder is not empty.

它并不是ruby特有的,但是由于bash在这种情况下更简单,所以我经常使用它来清理临时文件夹和文件。rm命令只删除您给它的任何内容,而-r标志告诉它递归地删除文件,以防文件夹不是空的。