如何在 Ruby 中创建文件
我正在尝试创建一个新文件,但事情似乎没有按照我的预期进行。这是我尝试过的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用:
您的选项是:
r
- 只读。该文件必须存在。w
- 创建一个空文件用于写入。a
- 附加到文件。如果文件不存在,则创建该文件。r+
- 打开文件以更新读取和写入。该文件必须存在。w+
- 创建一个空文件以供读取和写入。a+
- 打开文件进行读取和追加。如果文件不存在,则创建该文件。就您而言,
'w'
更可取。或者您可以:
...但这有忘记调用
close
的风险(例如,如果引发异常,或者您提前返回
)。Use:
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:
... but that has the risk of forgetting to call
close
(such as if an exception is raised, or youreturn
early).尝试
不使用
Try
without using the
尝试使用
"w+"
作为写入模式,而不仅仅是"w"
:Try using
"w+"
as the write mode instead of just"w"
:如果这对其他人有帮助,当您尝试在不存在的目录中创建新文件时,可能会发生这种情况。
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.
如果目标只是创建一个文件,我看到的最直接的方法是:
If the objective is just to create a file, the most direct way I see is:
File.new
和File.open
默认为读取模式 ('r'
) 作为安全机制,以避免可能覆盖文件。如果我们要输出到文件,我们必须明确告诉 Ruby 使用写入模式('w'
是最常见的方式)。如果要输出的文本是字符串,而不是 write:
或更糟:
使用更简洁的
write
:write
具有允许的模式,因此我们可以使用'w '
、'a'
、'r+'
(如有必要)。如果您必须在迭代循环中计算输出并希望在执行此操作时保持文件打开,则使用块的
open
非常有用。如果您要一次性输出内容然后关闭文件,则write
非常有用。有关更多信息,请参阅文档。
File.new
andFile.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:
or worse:
Use the more succinct
write
: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.
该目录不存在。确保它存在,因为
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.
您可以使用
File.write('此处的文件名', data)
You can use
File.write('name of file here', data)
您还可以使用常量而不是字符串来指定所需的模式。这样做的好处是,如果您在常量名称中输入错误,您的程序将引发运行时异常。
常量为
File::RDONLY
或File::WRONLY
或File::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
orFile::WRONLY
orFile::CREAT
. You can also combine them if you like.Full description of file open modes on ruby-doc.org
您可以尝试多种不同的选择。 (这并不是一个详尽的列表......只是累了。)
File.new
File.open
File.write
FileUtils.touch
IO.sysopen 和 IO.sysnew
临时文件
路径名和打开
图表来源:如何在 Ruby 中写入文件(披露:我写的)
There are several different options you could try. (This is not meant to be an exhaustive list... just tiring.)
File.new
File.open
File.write
FileUtils.touch
IO.sysopen and IO.sysnew
Tempfile
Pathname and open
Chart source: How to write to a file in Ruby (Disclosure: I wrote it)