[英]How to write output file of ruby code into specific directory/location?
I have ruby code which will create output file, currently file is creating at location from where my script is running. 我有ruby代码,它将创建输出文件,当前文件是在运行脚本的位置创建的。 I have to write output file to different location so i am specifying explicit path into code. 我必须将输出文件写入其他位置,所以我要在代码中指定显式路径。 but it's not able to create file. 但无法创建文件。 my code looks like : 我的代码看起来像:
fname = "C:\repo\cookbooks\abc\recipes\add.rb"
somefile = File.open(fname,"w")
somefile.puts "end"
somefile.close
If i specify 如果我指定
fname = "add.rb"
it's working but i want to create it at different location as i mentioned above code in C:\\ drive. 它正在工作,但我想在上面提到的C:\\驱动器中的上述代码的不同位置创建它。
Because \\
in strings are special characters so you should use \\\\
(double backslashes) to get a single backslash. 因为字符串中的\\
是特殊字符,所以您应该使用\\\\
(双反斜杠)来获得一个单反斜杠。 But there is a better way, you don't need to deal with backslashes at all: 但是有更好的方法,您根本不需要处理反斜杠:
fname = File.join("C:", "repo", "cookbooks", "abc", "recipes", "add.rb")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.