Errno::ENOENT: 没有那个文件或目录 ruby

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15605782/
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 05:50:34  来源:igfitidea点击:

Errno::ENOENT: No such file or directory ruby

ruby

提问by user2012677

I am getting the following error:

我收到以下错误:

"Errno::ENOENT: No such file or directory"

when I try to download a file into a directory that does not exist.

当我尝试将文件下载到不存在的目录时。

For example:

例如:

ftp = Net::FTP.new('example.com')
ftp.login
files = ftp.chdir('pub/lang/ruby/contrib')
files = ftp.list('n*')
ftp.getbinaryfile('nif.rb-0.91.gz', 'pub/lang/ruby/contrib/nif.gz', 1024)
ftp.close

However, for many of the files I will be downloading, the full directory path will not exist. For example, until the first file creates pub, it won't exist and this goes for lang/ruby/contrib too.

但是,对于我将要下载的许多文件,完整的目录路径将不存在。例如,在第一个文件创建 pub 之前,它不会存在,这也适用于 lang/ruby/contrib。

Is there a method that can check if directories exist and if not create them? I know that there is exist?, but that looks to reference files and it does not create the full path. I figure I need some recursive method to traverse down the folder structure until it hits the filename.

有没有一种方法可以检查目录是否存在,如果不存在则创建它们?我知道存在吗?,但这看起来是参考文件,它不会创建完整路径。我想我需要一些递归方法来遍历文件夹结构,直到遇到文件名。

EDIT: mkdir_p seems to be exactly the solution I need.

编辑: mkdir_p 似乎正是我需要的解决方案。

However, when I use a path of "/a/b/c" I noticed that FileUtils.mkdir_p(File.dirname(localdir)) returns an error: Errno::EACCES: Permission denied - /a

但是,当我使用“/a/b/c”路径时,我注意到 FileUtils.mkdir_p(File.dirname(localdir)) 返回错误:Errno::EACCES: Permission denied - /a

If I remove the "/" in front manually it works. What is causing this? What is the best solution for resolving the error? should I just use the following?

如果我手动删除前面的“/”,它会起作用。这是什么原因造成的?解决错误的最佳解决方案是什么?我应该只使用以下内容吗?

 path="/a/b/c"
 if path[0]="/"
    path=path[1..-1]
 end

However, I feel like this is just a hack.

但是,我觉得这只是一个黑客。

采纳答案by the Tin Man

You can't save to a non-existing path. As the developer you're responsible to make sure your destination paths exist prior to trying to save the file.

您无法保存到不存在的路径。作为开发人员,您有责任在尝试保存文件之前确保目标路径存在。

Ruby has methods that let you check for the existence a file or directory:

Ruby 有一些方法可以让您检查文件或目录是否存在:

FileUtils.makedirs()is an alias to mkpath, and mkdir_p. I prefer makedirsbecause it's a mnemonic.

FileUtils.makedirs()mkpath, 和的别名mkdir_p。我更喜欢,makedirs因为它是一个助记符。

These will also be useful:

这些也很有用:

Between those two I prefer Dir.chdir, because it takes a block. Any actions that occur inside that block happen in the directory you changed to. At exit, the directory reverts to what it was previously.

在这两者之间,我更喜欢Dir.chdir,因为它需要一个块。该块内发生的任何操作都发生在您更改的目录中。退出时,目录恢复到以前的状态。

回答by Jim Ford

The analog of *nix mkdir -p, mkdir_p, is what you need, which was provided in the other two answers, but I didn't see anyone address your edit about permissions.

*nix mkdir -p, mkdir_p 的模拟是您所需要的,在其他两个答案中提供,但我没有看到任何人解决您对权限的编辑。

The solution is the user invoking your script requires write permission on the root directory.

解决方案是调用您的脚本的用户需要对根目录的写权限。

The path, "/a/b/c", is addressing the root path ("/"). This is typically only write accessible to the root user or a user that has sudo access.

路径“/a/b/c”指向根路径(“/”)。这通常仅对 root 用户或具有 sudo 访问权限的用户具有写入权限。

When you modified the path to remove the leading "/", you told mkdir_p to create the directories "a/b/c" relative to the current working directory instead of relative to the root directory. The reason the modified version works is because the user invoking your ruby script has write permission on the current working directory.

当您修改路径以删除前导“/”时,您告诉 mkdir_p 创建相对于当前工作目录而不是相对于根目录的目录“a/b/c”。修改后的版本有效的原因是调用您的 ruby​​ 脚本的用户对当前工作目录具有写权限。

As a personal aside, if consumers of your script are providing the path input, then you probably don't want to remove leading slashes as that can create files/directories in a drastically different place from where the user expects...unless that is the purpose of your program, but make that clear up front.

就个人而言,如果您的脚本的使用者提供路径输入,那么您可能不想删除前导斜杠,因为这可能会在与用户期望的完全不同的地方创建文件/目录......除非那是你的程序的目的,但要事先说清楚。

References:

参考:

回答by gsumk

I got here because I had gem installed but the file was not installed on the machine. In my case, I had gem espeak-rubygem was installed but it was not installed on my mac so I installed that with simple brew install espeakcommand and restarted the server. My recommendation to you us make sure everything is installed properly.

我来到这里是因为我安装了 gem,但该文件没有安装在机器上。就我而言,我espeak-ruby安装了 gem gem,但它没有安装在我的 mac 上,所以我用简单的brew install espeak命令安装了它并重新启动了服务器。我给你的建议是确保一切都安装正确。

回答by Jorge Israel Pe?a

In the fileutils module you can use mkdir_pwhich is similar to the mkdir -pcommand which creates the full path if it doesn't already exist.

在 fileutils 模块中,您可以使用mkdir_p类似于mkdir -p创建完整路径的命令(如果尚不存在)。