【问题标题】:Ruby: No such file or directory @ rb_sysopen - testfile (Errno::ENOENT)Ruby:没有这样的文件或目录@ rb_sysopen - testfile (Errno::ENOENT)
【发布时间】:2022-02-20 05:11:11
【问题描述】:

我想写一些东西到一个文件中。

# where userid is any intger [sic]
path = Rails.root + "public/system/users/#{user.id}/style/img.jpg" 
File.open(path, 'wb') do |file|
  file.puts f.read
end 

执行此代码时,我收到此错误。我知道这个文件夹不存在,但是File.openw 模式会在它不存在时创建一个新文件。

为什么这不起作用?

【问题讨论】:

    标签: ruby file


    【解决方案1】:

    试图在 rake 任务中使用gets?您可能会看到以下错误消息:

    Errno::ENOENT: 没有这样的文件或目录@rb_sysopen

    您是否尝试搜索错误并最终出现在此页面上?这个答案不是给 OP 的,而是给你的。

    使用STDIN.gets。问题解决了。这是因为仅使用 gets 会解析回 $stdin.gets 并且 rake 会覆盖全局变量,因此 gets 会尝试打开不存在的文件句柄。原因如下:

    What's the difference between gets.chomp() vs. STDIN.gets.chomp()?

    【讨论】:

    • 这也适用于在 Vagrantfile 中使用gets
    【解决方案2】:

    File.open(..., 'w') 如果文件不存在,则创建该文件。没有人承诺它会为它创建一个目录树。

    另一件事,应该使用File#join 来构建目录路径,而不是愚蠢的字符串连接。

    path = File.join Rails.root, 'public', 'system', 'users', user.id.to_s, 'style'
    
    FileUtils.mkdir_p(path) unless File.exist?(path) 
    File.open(File.join(path, 'img.jpg'), 'wb') do |file|
      file.puts f.read
    end
    

    【讨论】:

    • 只是好奇:为什么 File.join 在这里是可取的?它总是将路径组件与平台特定的分隔符连接起来(例如,` on Windows). While there **are** cases, where this is what we want to achive, we usually try to stick with /` 在所有平台上,因为这在可移植性方面不那么令人头疼。在此处发布的代码的情况下,我不明白为什么平台- 特定的文件分隔符可能是一个优势。
    • @user1934428 “我们通常会尝试在所有平台上坚持使用 /,因为这样可以减少便携性方面的麻烦”——除了这是无稽之谈,使用 File.join 有助于避免类似的愚蠢错误在 OP 中:Rails.root 不以斜线结尾,使用 + 给出 /railsrootpublic/(请注意上面的斜线未命中。)
    • @mudasobwa: 在我的 rails 控制台 Rails.root + "bla/bla" 给出正确的路径:)
    • @mudasobwa :它有效,但我必须进行一些更改,例如 Dir.mkdir 只创建目录而不是目录树,我使用 FileUtils.mkdir_p :谢谢帮助
    【解决方案3】:

    File.open(..., 'w') 无法打开文件(即由于 Windows 10 下的文件夹访问受保护)时,您也会收到此错误。

    【讨论】:

      【解决方案4】:

      在我的情况下是因为在上面的方法中我有:

      Dir.chdir(some_folder)
      

      所以它会更改当前目录以供下一个 File.openFile.readlines 来电。

      【讨论】:

        【解决方案5】:

        我在尝试在 Ruby 中创建文件时遇到了这个问题。

        我试图使用下面的命令来创建一个新文件:

        File.new("testfile", "r")
        

        File.open("testfile", "r")
        

        但后来我收到以下错误:

        (irb):1:in `initialize': 没有这样的文件或目录@rb_sysopen - testfile (Errno::ENOENT)

        这是我修复它的方法

        问题是我没有为文件指定正确的模式。创建新文件的格式为:

        File.new(filename, mode)
        

        File.open(filename, mode)
        

        各种模式分别是:

        "r"  Read-only, starts at beginning of file  (default mode).
        
        "r+" Read-write, starts at beginning of file.
        
        "w"  Write-only, truncates existing file
             to zero length or creates a new file for writing.
        
        "w+" Read-write, truncates existing file to zero length
             or creates a new file for reading and writing.
        
        "a"  Write-only, each write call appends data at end of file.
             Creates a new file for writing if file does not exist.
        
        "a+" Read-write, each write call appends data at end of file.
             Creates a new file for reading and writing if file does
             not exist.
        

        但是,我的命令 File.new("testfile", "r") 正在使用 "r" Read-only 模式,该模式试图从名为 testfile 的现有文件中读取,而不是创建新文件。我所要做的就是修改命令以使用"w" Write-only 模式:

        File.new("testfile", "w")
        

        File.open("testfile", "w")
        

        参考File in Ruby

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-09
          • 2018-05-01
          • 2018-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多