Ruby(1.8.1)-ERRNO :: EEXIST文件存在

时间:2018-12-12 19:41:08

标签: ruby file rpgmaker-xp

所以我正在使用Ruby,并且... 我有一个错误ERRNO::EEXIST, Script 'text' line 2 ... File exists 如果它创建的目录(在EGGPATH中定义)已经存在,则会发生错误,由于unless File.exist?(EGGPATH)本身应该可以工作 我想做的是:

def cyhm   #file text
  Dir.mkdir(EGGPATH) unless File.exist?(EGGPATH)
  File.open(EGGPATH + "/CAN YOU HEAR ME.txt", "w+") do |f|
    f.write("text")
  end
end
def cyhm_pt2
  Dir.mkdir(EGGPATH) unless File.exist?(EGGPATH)
  if File.exists?(EGGPATH + "/CAN YOU HEAR ME.txt") == false
    cyhm
  end
  File.open(EGGPATH + "/CAN YOU HEAR ME.txt", "a+") do |f|
    f.write("text2")
  end
end

在此处在其他文件中定义了EGGPATH的情况:

TXTLIST = ["Ą", "ą", "Ć", "ć", "Ę", "ę", "Ł", "ł", "Ń", "ń", "Ó", "ó", "Ś", 
"ś", "Ź", "ź", "Ż", "ż"] #file randomtxt
EGGPAT = TXTLIST[rand(TXTLIST.size)] + TXTLIST[rand(TXTLIST.size)] + 
TXTLIST[rand(TXTLIST.size)] + TXTLIST[rand(TXTLIST.size)] + 
TXTLIST[rand(TXTLIST.size)] + TXTLIST[rand(TXTLIST.size)] + 
TXTLIST[rand(TXTLIST.size)] + TXTLIST[rand(TXTLIST.size)] + 
TXTLIST[rand(TXTLIST.size)] + TXTLIST[rand(TXTLIST.size)] + 
TXTLIST[rand(TXTLIST.size)]
if File.exists?(SAVEPATH + "\\MODAR\\eseggdir.es") == false
  File.open(SAVEPATH + "\\MODAR\\eseggdir.es", "w+") do |f|
    f.write(EGGPAT)
  end
end
EGGPATH = File.read(SAVEPATH + "\\MODAR\\eseggdir.es")

另外,不要告诉我切换到较新版本的Ruby,因为我正在使用RPGXP,并且我不想很快更改它...

2 个答案:

答案 0 :(得分:0)

由于目录MODAR在您的系统上不存在,因此出现此错误。像下面这样的东西应该解决它:

modar_path = File.join(SAVEPATH, 'MODAR')

unless Dir.exists?(modar_path)
  Dir.mkdir(modar_path)
end

还有另一条可能有效的命令:

require 'fileutils'
# replace modar_path with the path you choose, or set it as I did above
FileUtils.mkdir_p(modar_path)

答案 1 :(得分:0)

所以,我发现Ruby无法忍受检查文件名中是否包含英语字母以外的其他字符。耻辱。这就是解决方案,我将不得不重新创建TXTLIST数组以: TXTLIST = ["A", "b", "C", "D", "e", "F", "g", "H", "i", "J", "k", "L", "m", "N", "o", "P", "r", "S", "t", "U", "w", "y", "X", "z", "Q", "v", "a", "B", "c", "d", "E", "f", "G", "h", "I", "j" "K", "l", "M", "n", "O", "p", "R", "r", "S", "t", "U", "w", "Y", "x", "Z", "q", "V"]或类似的东西。