【问题标题】:Method Dir.mkdir throws Errno::EACCES方法 Dir.mkdir 抛出 Errno::EACCES
【发布时间】:2013-11-18 16:26:29
【问题描述】:

我的代码中有一个奇怪的、反复出现的错误。很少,方法Dir.mkdir 抛出`mkdir': Permission denied - <file_name> (Errno::EACCES),令人惊讶的是,有时它只是工作(没有任何单一的代码更改或任何东西)。

代码如下:

    data.each_with_index do |row, index|

      #Make directory for each row:
      row_name = "row_#{index}";
       #Dir.rmdir just in-case, same error occurs without it.
      (FileUtils.rm_r(row_name) || Dir.rmdir(row_name)) if File.exist?(row_name) #Remove previous directory
      Dir.mkdir(row_name);
      row.each_with_index do |(key, value), index|
        #Make file for every record
        File.open("#{row_name}/#{index}.rtf", 'w') do |f|

        f.write value

        f.close #Just in-case, even though it's automatically called(no change without it)

        end    
      end
  end  

错误很少发生(大约每执行 4 次此代码)。

其他:

  • Windows 7 平台
  • 执行时未访问任何文件
  • 以管理员身份使用 CMD。

为什么即使没有对代码进行任何更改,我也会收到此错误?
会不会是目录创建过多?(每次执行平均会创建~60个目录)

【问题讨论】:

    标签: ruby file io permissions


    【解决方案1】:

    问题是由于操作系统阻止脚本过快删除文件引起的,解决方法是在进入循环之前删除所有文件。完美运行。

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 2015-07-20
      • 2016-03-14
      • 2014-01-24
      相关资源
      最近更新 更多