如何在 Ruby 中创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7911669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:25:33  来源:igfitidea点击:

How to create a file in Ruby

rubyfileioerrno

提问by Civatrix

I'm trying to create a new file and things don't seem to be working as I expect them too. Here's what I've tried:

我正在尝试创建一个新文件,但事情似乎也没有像我期望的那样工作。这是我尝试过的:

File.new "out.txt"
File.open "out.txt"
File.new "out.txt","w"
File.open "out.txt","w"

According to everything I've read online all of those should work but every single one of them gives me this:

根据我在网上阅读的所有内容,所有这些都应该有效,但每一个都给了我这个:

ERRNO::ENOENT: No such file or directory - out.txt

This happens from IRB as well as a Ruby script. What am I missing?

这发生在 IRB 以及 Ruby 脚本中。我错过了什么?

回答by zanbri

Use:

用:

File.open("out.txt", [your-option-string]) {|f| f.write("write your stuff here") }

where your options are:

您的选择是:

  • r- Read only. The file must exist.
  • w- Create an empty file for writing.
  • a- Append to a file.The file is created if it does not exist.
  • r+- Open a file for update both reading and writing. The file must exist.
  • w+- Create an empty file for both reading and writing.
  • a+- Open a file for reading and appending. The file is created if it does not exist.
  • r- 只读。该文件必须存在。
  • w- 创建一个空文件用于写入。
  • a- 附加到文件。如果文件不存在,则创建该文件。
  • r+- 打开文件以进行读写更新。该文件必须存在。
  • w+- 为读取和写入创建一个空文件。
  • a+- 打开文件进行阅读和追加。如果文件不存在,则创建该文件。

In your case, 'w'is preferable.

在你的情况下,'w'是可取的。

OR you could have:

或者你可以有:

out_file = File.new("out.txt", "w")
#...
out_file.puts("write your stuff here")
#...
out_file.close

回答by GMD

Try

尝试

File.open("out.txt", "w") do |f|     
  f.write(data_you_want_to_write)   
end

without using the

不使用

File.new "out.txt"

回答by Chris Bunch

Try using "w+"as the write mode instead of just "w":

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

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

回答by tom

OK, now I feel stupid. The first two definitely do not work but the second two do. Not sure how I convinced my self that I had tried them. Sorry for wasting everyone's time.

好吧,现在我觉得自己很愚蠢。前两个绝对行不通,但后两个确实行。不知道我是如何说服自己我尝试过它们的。抱歉耽误了大家的时间。

In case this helps anyone else, this can occur when you are trying to make a new file in a directory that does not exist.

如果这对其他人有帮助,当您尝试在不存在的目录中创建新文件时可能会发生这种情况。

回答by nterry

The directory doesn't exist. Make sure it exists as openwon't create those dirs for you.

该目录不存在。确保它存在,因为open不会为您创建这些目录。

I ran into this myself a while back.

不久前我自己遇到了这个问题。

回答by the Tin Man

File.newand File.opendefault to read mode ('r') as a safety mechanism, to avoid possibly overwriting a file. We have to explicitly tell Ruby to use write mode ('w'is the most common way) if we're going to output to the file.

File.newFile.open默认为读取模式 ( 'r') 作为一种安全机制,以避免可能覆盖文件。'w'如果我们要输出到文件,我们必须明确告诉 Ruby 使用写入模式(这是最常见的方式)。

If the text to be output is a string, rather than write:

如果要输出的文本是字符串,而不是写:

File.open('foo.txt', 'w') { |fo| fo.puts "bar" }

or worse:

或更糟的是:

fo = File.open('foo.txt', 'w')
fo.puts "bar"
fo.close

Use the more succinct write:

使用更简洁的write

File.write('foo.txt', 'bar')

writehas modes allowed so we can use 'w', 'a', 'r+'if necessary.

write已经允许的模式,所以我们可以使用'w''a''r+'如果需要的话。

openwith a block is useful if you have to compute the output in an iterative loop and want to leave the file open as you do so. writeis useful if you are going to output the content in one blast then close the file.

open如果您必须在迭代循环中计算输出并希望在执行此操作时保持文件打开,则使用块很有用。write如果您要一次性输出内容然后关闭文件,这将很有用。

See the documentationfor more information.

有关更多信息,请参阅文档

回答by Nicola Mingotti

If the objective is just to create a file, the most direct way I see is:

如果目标只是创建一个文件,我看到的最直接的方法是:

 FileUtils.touch "foobar.txt"

回答by ispirett

data = 'data you want inside the file'.

You can use File.write('name of file here', data)

您可以使用 File.write('name of file here', data)

回答by Alex Tamoykin

You can also use constants instead of strings to specify the mode you want. The benefit is if you make a typo in a constant name, your program will raise an runtime exception.

您还可以使用常量而不是字符串来指定所需的模式。好处是如果您在常量名称中打错了字,您的程序将引发运行时异常。

The constants are File::RDONLYor File::WRONLYor File::CREAT. You can also combine them if you like.

常量是File::RDONLYorFile::WRONLYFile::CREAT。如果您愿意,您也可以将它们组合起来。

Full description of file open modes on ruby-doc.org

ruby-doc.org 上文件打开模式的完整描述