在Windows 7上的错误Errno :: ENOENT:

时间:2014-04-26 12:01:34

标签: ruby

我需要在ruby提示符上发出命令

irb(main):007:0> f = File.new("C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutp>
Errno::ENOENT: No such file or directory - ls -alF C:UsersABHIJI\~1.SHEAppDataLo
calTemp\myoutput.txt
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/formatter.rb:191:in ``'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/formatter.rb:191:in `awesome_file'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/formatter.rb:26:in `format'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/inspector.rb:137:in `unnested'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/inspector.rb:104:in `awesome'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/core_ext/kernel.rb:10:in `ai'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/core_ext/kernel.rb:15:in `ap'
        from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw
esome_print/inspector.rb:31:in `output_value'
        from C:/Ruby/Ruby193/bin/irb:12:in `<main>'

我已经给出了temp文件夹的路径。 我使用的是Windows 7,Ruby路径是C:\ Ruby \ Ruby193 \ bin

1 个答案:

答案 0 :(得分:1)

反斜杠字符用作“转义字符”,用于在字符串中插入特殊字符。例如,要插入制表位,则使用“\ t”。如果你想要一个文字\,你必须用另一个\来逃避它。所以你的字符串应该是"C:\\Users\\ABHIJI~1.SHE\\AppData\\Local\\Temp\\myoutput.txt"

示例:

# right
>> puts "C:\\Users\\ABHIJI~1.SHE\\AppData\\Local\\Temp\\myoutput.txt"
C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutput.txt
=> nil

# wrong
>> puts "C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutput.txt"
C:UsersABHIJI~1.SHEAppDataLocalTempmyoutput.txt
=> nil

但是,您也可以在路径中使用正斜杠,即使在Windows上也是如此。有关详细信息,请参阅此答案https://stackoverflow.com/a/14875567/1067124