如何在 Ruby 中使用一个命令检查目录/文件/符号链接是否存在
是否有一种方法可以检测目录/文件/符号链接/等是否存在。实体(更广义)存在吗?
我需要一个函数,因为我需要检查路径数组,这些路径可能是目录、文件或符号链接。我知道 File.exists?"file_path"
适用于目录和文件,但不适用于符号链接(即 File.symlink?"symlink_path"
)。
Is there a single way of detecting if a directory/file/symlink/etc. entity (more generalized) exists?
I need a single function because I need to check an array of paths that could be directories, files or symlinks. I know File.exists?"file_path"
works for directories and files but not for symlinks (which is File.symlink?"symlink_path"
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准文件模块具有常用的文件测试:
您应该能够在那里找到你想要的东西。
显然不是。好的,尝试一下:
The standard File module has the usual file tests available:
You should be able to find what you want there.
Obviously not. Ok, try something like:
为什么不定义自己的函数
File.exists?(path) 或 File.symlink?(path)
并使用它呢?Why not define your own function
File.exists?(path) or File.symlink?(path)
and use that?只需
File.exist?
本身即可为您处理上述所有问题Just
File.exist?
on it's own will take care of all of the above for you