3

我完全被这个难住了。我正在尝试调试一些创建FirstRun文件的代码,作为程序已经完成第一次运行设置的标记。它尝试在absworkingdir变量给定的指定工作目录中创建它。但是,当它尝试使用创建文件时File.new出现以下错误

`initialize': No such file or directory '

以下是相关代码:

#First run setup
puts workingdir#debug
absworkingdir = File.expand_path(workingdir)
puts absworkingdir#debug
if File.exist?("#{absworkingdir}/FirstRun.lock") == false
    puts "This appears to be the first run of FigShare Sync. We'll setup a few things."
    print "Where would you like to store settings and files? [~./figsharesync]: "
    @input = gets.chomp
    puts @input#debug
        if @input.empty? == false
            workingdir = @input
            absworkingdir = File.expand_path(workingdir)
        end
    print "Please enter OAuth consumer key: "
    consumerkey = gets.chomp
    print "Please enter OAuth consumer key secret: "
    consumersecret = gets.chomp
    print "Please enter OAuth access token: "
    accesstoken = gets.chomp
    print "Please enter OAuth access token secret: "
    accesstokensecret = gets.chomp
    puts "Great! we'll get running now..."
puts absworkingdir#debug
File.new("#{absworkingdir}/FirstRun.lock", "r")
end

这是调试行的输出:

~/.figsharesync/                                                                                                                                                                                                                        
/var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync                                                                                                                                                                
This appears to be the first run of FigShare Sync. We'll setup a few things.                                                                                                                                                            
Where would you like to store settings and files? [~./figsharesync]:                                                                                                                                                                    

Please enter OAuth consumer key:                                                                                                                                                                                                        
Please enter OAuth consumer key secret:                                                                                                                                                                                                 
Please enter OAuth access token:                                                                                                                                                                                                        
Please enter OAuth access token secret:                                                                                                                                                                                                 
Great! we'll get running now...                                                                                                                                                                                                         
/var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync                                                                                                                                                                
source/figsharesync2.rb:38:in `initialize': No such file or directory - /var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync/FirstRun.lock(Errno::ENOENT)                                                          
    from source/figsharesync2.rb:38:in `new'                                                                                                                                                                                        
    from source/figsharesync2.rb:38:in `<main>'     
4

4 回答 4

6

你想对 File.new 使用写模式,否则你说你想以读模式打开一个不存在的文件:

f = File.new("#{absworkingdir}/FirstRun.lock", "w")
f.close
于 2013-04-24T19:42:37.680 回答
0

您处于条件为“如果此文件不存在”的 if 块中,然后您尝试打开不存在的文件进行读取。那显然行不通。您可能想打开写作而不是阅读。

于 2013-04-24T19:42:12.333 回答
0

您没有/var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync/FirstRun.lock要打开以供阅读的文件。如果您尝试创建要写入的新文件,则需要使用模式"w"(write),如果文件不存在则创建文件,而不是"r"(read),后者要求文件已经存在。

有关这些模式的更多信息,请参阅文档。还有http://www.ruby-doc.org/core-1.9.3/File.html

于 2013-04-24T19:42:22.270 回答
0

尝试使用 w+ 作为写入模式,而不仅仅是 w:

File.open("out.txt", "w+") { |file| file.write("boo!") }

请参阅:如何在 Ruby 中创建文件

于 2014-03-12T10:49:47.840 回答