ruby 如何检查文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8590098/
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:36:15 来源:igfitidea点击:
How to check for file existence
提问by iwan
Is there a Ruby class/method where I could pass "a full path", home/me/a_file.txt, to identify whether it is a valid file path?
是否有 Ruby 类/方法可以传递“完整路径” home/me/a_file.txt,以识别它是否是有效的文件路径?
采纳答案by Paul Annesley
Check out Pathnameand in particular Pathname#exist?.
查看Pathname尤其是Pathname#exist?.
Fileand its FileTestmodule are perhaps simpler/more direct, but I find Pathnamea nicer interface in general.
回答by zed_0xff
# file? will only return true for files
File.file?(filename)
and
和
# Will also return true for directories - watch out!
File.exist?(filename)

