如何检查Ruby中是否存在给定的目录?

时间:2021-09-07 11:25:35

I am trying to write a script which automatically checks out or updates a Subversion URL based on whether a specified directory exists or not.

我正在尝试编写一个脚本,该脚本根据指定目录是否存在自动检出或更新Subversion URL。

For some reason, my code isn't working and always returns true even if it's false:

由于某些原因,我的代码不能正常工作,即使它是假的,也总是返回true:

def directory_exists?(directory)
  return false if Dir[directory] == nil
  true
end

What am I doing wrong?

我做错了什么?

5 个解决方案

#1


245  

If it matters whether the file you're looking for is a directory and not just a file, you could use File.directory? or Dir.exist?. This will return true only if the file exists and is a directory.

如果您要查找的文件是一个目录而不只是一个文件,那么您可以使用file .directory?还是Dir.exist ?。只有当文件存在且为目录时,才返回true。

As an aside, a more idiomatic way to write the method would be to take advantage of the fact that Ruby automatically returns the result of the last expression inside the method. Thus, you could write it like this:

顺便说一下,编写该方法的一个更惯用的方法是利用Ruby自动返回方法中最后一个表达式的结果的事实。因此,你可以这样写:

def directory_exists?(directory)
  File.directory?(directory)
end

Note that using a method is not necessary in the present case.

注意,在本例中不需要使用方法。

#2


32  

All the other answers are correct, however, you might have problems if you're trying to check directory in a user's home directory. Make sure you expand the relative path before checking:

所有其他答案都是正确的,但是,如果要检查用户主目录中的目录,可能会遇到问题。在检查之前,请确保扩展了相关路径:

File.exists? '~/exists'
=> false
File.directory? '~/exists'
=> false
File.exists? File.expand_path('~/exists')
=> true

#3


30  

You can also use Dir::exist? like so:

您还可以使用Dir: exist?像这样:

Dir.exist?('Directory Name')

Returns true if the 'Directory Name' is a directory, false otherwise.1

如果“目录名”是一个目录,则返回true

#4


15  

File.exist?("directory")

Dir[] returns an array, so it will never be nil. If you want to do it your way, you could do

Dir[]返回一个数组,因此它永远不会是nil。如果你想按自己的方式去做,你就能做到。

Dir["directory"].empty?

which will return true if it wasn't found.

如果没有找到,它将返回true。

#5


5  

You could use Kernel#test:

您可以使用内核#测试:

test ?d, 'some directory'

#1


245  

If it matters whether the file you're looking for is a directory and not just a file, you could use File.directory? or Dir.exist?. This will return true only if the file exists and is a directory.

如果您要查找的文件是一个目录而不只是一个文件,那么您可以使用file .directory?还是Dir.exist ?。只有当文件存在且为目录时,才返回true。

As an aside, a more idiomatic way to write the method would be to take advantage of the fact that Ruby automatically returns the result of the last expression inside the method. Thus, you could write it like this:

顺便说一下,编写该方法的一个更惯用的方法是利用Ruby自动返回方法中最后一个表达式的结果的事实。因此,你可以这样写:

def directory_exists?(directory)
  File.directory?(directory)
end

Note that using a method is not necessary in the present case.

注意,在本例中不需要使用方法。

#2


32  

All the other answers are correct, however, you might have problems if you're trying to check directory in a user's home directory. Make sure you expand the relative path before checking:

所有其他答案都是正确的,但是,如果要检查用户主目录中的目录,可能会遇到问题。在检查之前,请确保扩展了相关路径:

File.exists? '~/exists'
=> false
File.directory? '~/exists'
=> false
File.exists? File.expand_path('~/exists')
=> true

#3


30  

You can also use Dir::exist? like so:

您还可以使用Dir: exist?像这样:

Dir.exist?('Directory Name')

Returns true if the 'Directory Name' is a directory, false otherwise.1

如果“目录名”是一个目录,则返回true

#4


15  

File.exist?("directory")

Dir[] returns an array, so it will never be nil. If you want to do it your way, you could do

Dir[]返回一个数组,因此它永远不会是nil。如果你想按自己的方式去做,你就能做到。

Dir["directory"].empty?

which will return true if it wasn't found.

如果没有找到,它将返回true。

#5


5  

You could use Kernel#test:

您可以使用内核#测试:

test ?d, 'some directory'