I have a program that reads the files in the directory and outputs the contents of the files.
in `read': Isa directory
The error appears.
I think you're probably saying, "That's a directory," but since you've verified the file in the previous if
statement, it should be the path of the text file, not the directory.
Why does this error occur?
require "find.rb"
Find::find("/Users/tamada/Desktop/Review Dev/Recipe Data Perspective Material/Test Data/recipe/") do | path |
ifFileTest.file?(path)||FileTest.readable?(path)then
# p "File: " + path
file=open(path)
text=file.read
ptext
file.close
end
end
(The "Resolved" answer has been deleted.) If it has been resolved, you should state what the problem was.
Perhaps the FileTest.readable?(path)
statement was unnecessary.
Find.find
method enumerates files below the directory for arguments.Include the directory itself.FileTest.#readable?
method returns whether or not you have read permission, so the directory also returns true
.I'm sorry, but I refactored it.
require "find"
Find.find("/Users/tamada/Desktop/Review Dev/Recipe Data Perspective Material/Test Data/recipe/") do | path |
ifFileTest.file?(path)
open(path)do|f|
puts f.read
end
end
end
List the points.
Kernel#require
method argument is library name only and is abbreviated with an extension (by contrast Kernel#load
is required).::
such as Find::find
.Find.find
is common (but it was an old notation in the recent book "Effective Ruby" ^^;)then
in the if
statement.Kernel#open
method, Ruby is the way to use blocks instead of using them because you tend to forget to close them.p
is a debugging-only method.Use puts
for output (although it may still be in the middle of debugging).file.read
/f.read
as the argument for p
/puts
instead of using the
© 2024 OneMinuteCode. All rights reserved.