ruby 空目录(删除所有文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8224260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:31:41  来源:igfitidea点击:

Empty directory (delete all files)

ruby

提问by Pablo

What would be a safe and efficient way to delete all files in a directory in pure Ruby? I wrote

在纯 Ruby 中删除目录中所有文件的安全有效方法是什么?我写

Dir.foreach(dir_path) {|f| File.delete(f) if f != '.' && f != '..'}

but it gives me a No such file or directoryerror.

但它给了我一个No such file or directory错误。

Thank you.

谢谢你。

采纳答案by sarnold

You're probably getting that error because your current working directory doesn't match dir_path-- File.delete(f)is being given just the filename for a file in dir_path. (I hope you didn't have any important files in the current working directory with same names in the dir_pathdirectory.)

您可能会收到该错误,因为您当前的工作目录不匹配dir_path-File.delete(f)只给出了dir_path. (我希望您在当前工作目录中没有任何同名的重要文件dir_path。)

You need to use File.join(dir_path, f)to construct the filename you wish to delete. You also need to figure out how you want to handle directories:

您需要使用File.join(dir_path, f)来构造要删除的文件名。您还需要弄清楚如何处理目录:

Dir.foreach(dir_path) do |f|
  fn = File.join(dir_path, f)
  File.delete(fn) if f != '.' && f != '..'
end

Errno::EISDIR: Is a directory - /tmp/testing/blubber
    from (irb):10:in `delete'
    from (irb):10
    from (irb):10:in `foreach'
    from (irb):10
    from :0

回答by Mario Uher

What about FileUtils.rm_rf("#{dir_path}/.", secure: true)?

怎么样FileUtils.rm_rf("#{dir_path}/.", secure: true)

回答by MyroslavN

FileUtils.rm_rf Dir.glob("#{dir_path}/*") if dir_path.present?

回答by denis.peplin

Everybody suggest rm_rf, but the safer way is to use rm_f, which is an alias to rm force: true.

大家建议rm_rf,但更安全的方法是使用rm_f,它是 的别名rm force: true

FileUtils.rm_f Dir.glob("#{dir_path}/*")

This method will not remove directories, it will only remove files: http://ruby-doc.org/stdlib-2.2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-rm

此方法不会删除目录,只会删除文件:http: //ruby-doc.org/stdlib-2.2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-rm

回答by tcurdt

The Tin Man almost got it right - but in case the directories are not empty use

铁皮人几乎做对了 - 但如果目录不是空的,请使用

Pathname.new(dir_path).children.each { |p| p.rmtree }

回答by LiamD

To tack on to MyroslavN, if you wanted to do it with Pathname (like with Rails.root, if it's a rails app) you want this:

继续使用 MyroslavN,如果你想用 Pathname(比如 Rails.root,如果它是一个 rails 应用程序)你想要这个:

FileUtils.rm_rf Dir.glob(Rails.root.join('foo', 'bar', '*'))

I'm posting this because I did a bad copypaste of his answer, and I accidentally wound up with an extra slash in my path:

我发布这个是因为我对他的回答做了错误的复制粘贴,而且我不小心在我的路径中添加了一个额外的斜线:

Rails.root.join('foo', 'bar', '/*')

Which evaluates to /*because it sees it as a root path. And if you put that in the FileUtils.rm_rf Dir.globit will attempt to recursively delete everything it can (probably limited by your permissions) in your filesystem.

其评估为,/*因为它将其视为根路径。如果你把FileUtils.rm_rf Dir.glob它放进去,它会尝试递归地删除你文件系统中的所有东西(可能受你的权限限制)。

回答by the Tin Man

Now days, I like using the Pathnameclass when working on files or directories.

现在,我喜欢在处理文件或目录时使用Pathname类。

This should get you started:

这应该让你开始:

Pathname.new(dir_path).children.each { |p| p.unlink }

unlinkwill remove directories or files, so if you have nested directories you want to preserve you'll have to test for them:

unlink将删除目录或文件,因此如果您有要保留的嵌套目录,则必须对其进行测试:

Removes a file or directory, using File.unlink if self is a file, or Dir.unlink as necessary.

删除文件或目录,如果 self 是文件,则使用 File.unlink ,或根据需要使用 Dir.unlink 。

回答by Ali Akbar

Dir.foreach(dir_path) {|f| File.delete("#{dir_path}/#{f}") if f != '.' && f != '..'}