如何在 Ruby 中创建文件

发布于 2024-12-12 01:38:45 字数 319 浏览 0 评论 0原文

我正在尝试创建一个新文件,但事情似乎没有按照我的预期进行。这是我尝试过的:

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

根据我在网上读到的所有内容,所有这些都应该有效,但它们中的每一个都给了我这样的结果:

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

这种情况发生在 IRB 以及 Ruby 脚本中。我缺少什么?

I'm trying to create a new file and things don't seem to be working as I expect them to. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

秋日私语 2024-12-19 01:38:45

使用:

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

您的选项是:

  • r - 只读。该文件必须存在。
  • w - 创建一个空文件用于写入。
  • a - 附加到文件。如果文件不存在,则创建该文件。
  • r+ - 打开文件以更新读取和写入。该文件必须存在。
  • w+ - 创建一个空文件以供读取和写入。
  • a+ - 打开文件进行读取和追加。如果文件不存在,则创建该文件。

就您而言,'w' 更可取。

或者您可以:

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

...但这有忘记调用 close 的风险(例如,如果引发异常,或者您提前返回)。

Use:

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

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.

In your case, 'w' is preferable.

OR you could have:

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

... but that has the risk of forgetting to call close (such as if an exception is raised, or you return early).

平生欢 2024-12-19 01:38:45

尝试

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

不使用

File.new "out.txt"

Try

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

without using the

File.new "out.txt"
叹沉浮 2024-12-19 01:38:45

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

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

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

File.open("out.txt", "w+") { |file| file.write("boo!") }
情未る 2024-12-19 01:38:45

好吧,现在我觉得自己很蠢。前两个肯定不起作用,但后两个可以。不知道我是如何说服自己我已经尝试过它们的。抱歉浪费了大家的时间。

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

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.

半﹌身腐败 2024-12-19 01:38:45

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

 FileUtils.touch "foobar.txt"

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

 FileUtils.touch "foobar.txt"
寂寞清仓 2024-12-19 01:38:45

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

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

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

或更糟:

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

使用更简洁的 write

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

write 具有允许的模式,因此我们可以使用 'w ''a''r+'(如有必要)。

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

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

File.new and File.open default 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.

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:

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

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

open with 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. write is useful if you are going to output the content in one blast then close the file.

See the documentation for more information.

十年九夏 2024-12-19 01:38:45

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

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

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

I ran into this myself a while back.

颜漓半夏 2024-12-19 01:38:45
data = 'data you want inside the file'.

您可以使用 File.write('此处的文件名', data)

data = 'data you want inside the file'.

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

爱你是孤单的心事 2024-12-19 01:38:45

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

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

文件打开模式的完整说明ruby-doc.org

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::RDONLY or File::WRONLY or File::CREAT. You can also combine them if you like.

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

以可爱出名 2024-12-19 01:38:45

您可以尝试多种不同的选择。 (这并不是一个详尽的列表......只是累了。)

File.new

file_name = File.new("out.txt", "w")
file_name.close

File.open

File.open("out.txt", "w") do |file|
  #other stuff here
end

File.write

File.write('out.txt', 'contents here')

FileUtils.touch

require 'fileutils'
FileUtils.touch('out.txt')

IO.sysopen 和 IO.sysnew

fd = IO.sysopen("out.txt", "w")
file = IO.new(fd, "w")
file.puts "other stuff"
file.close

临时文件

require 'tempfile'
tempfile = Tempfile.new('out')
tempfile.puts "some text"
tempfile.close

路径名和打开

require 'pathname'
path = Pathname.new("out.txt")
path.open("w") do |file|
  file.puts "some text"
end

图表来源:如何在 Ruby 中写入文件(披露:我写的)

Ruby 中创建文件的各种方法

There are several different options you could try. (This is not meant to be an exhaustive list... just tiring.)

File.new

file_name = File.new("out.txt", "w")
file_name.close

File.open

File.open("out.txt", "w") do |file|
  #other stuff here
end

File.write

File.write('out.txt', 'contents here')

FileUtils.touch

require 'fileutils'
FileUtils.touch('out.txt')

IO.sysopen and IO.sysnew

fd = IO.sysopen("out.txt", "w")
file = IO.new(fd, "w")
file.puts "other stuff"
file.close

Tempfile

require 'tempfile'
tempfile = Tempfile.new('out')
tempfile.puts "some text"
tempfile.close

Pathname and open

require 'pathname'
path = Pathname.new("out.txt")
path.open("w") do |file|
  file.puts "some text"
end

Chart source: How to write to a file in Ruby (Disclosure: I wrote it)

Various methods for file creation in Ruby

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文