Cómo comprobar la existencia de un archivo [duplicado]


Esta pregunta ya tiene una respuesta aquí:

¿Hay una clase/método Ruby donde pueda pasar "una ruta completa", home/me/a_file.txt, para identificar si es una ruta de archivo válida?

 186
Author: the Tin Man, 2011-12-21

2 answers

Echa un vistazo a Pathname y en particular Pathname#exist?.

File y su módulo FileTest son quizás más simples/más directos, pero encuentro Pathname una interfaz más agradable en general.

 46
Author: Paul Annesley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-15 17:35:09
# file? will only return true for files
File.file?(filename)

Y

# Will also return true for directories - watch out!
File.exist?(filename)
 480
Author: zed_0xff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-01 19:04:27