为什么许多语言中的“文件存在”方法对于目录都返回true?

时间:2022-07-12 05:30:40

I know that it does in PHP, and I'm pretty sure it does in Java. I haven't used the latest versions of .NET, so I won't speak for them. It seems very awkward, but I was wondering if there was an underlying reason for this.

我知道它在PHP中有用,我很确定它在Java中有用。我没有使用最新版本的.NET,所以我不会说它们。这看起来很尴尬,但我想知道是否有一个潜在的原因。

4 个解决方案

#1


3  

One reason is compatibility - anyone who has done 'check for existence' knows to exclude directories; changing that behaviour may confuse those who rely on that behaviour.

一个原因是兼容性 - 任何做过'检查存在'的人都知道要排除目录;改变这种行为可能会使那些依赖这种行为的人感到困惑。

Secondly, the underlying code often does a check on the operating system for existence in a catlog of filesystem entries - to the OS, a directory is the same as a file. In other words, it's looking for an entry of 'xyz' in the catalog not a file with name 'xyz' in the catalog.

其次,底层代码经常检查操作系统是否存在于文件系统条目的catlog中 - 对于OS,目录与文件相同。换句话说,它正在目录中寻找“xyz”条目,而不是目录中名为“xyz”的文件。

Backwards compatability is the main reason, I suspect.

我怀疑,向后兼容性是主要原因。

#2


2  

There is also a formal reason why a directory is a file:

目录是文件的正式原因如下:

Files (or links to files) can be located in directories. However, more generally, a directory can contain either a list of files or a list of links to files. Within this definition, it is of paramount importance that the term "file" includes directories. This permits the existence of directory hierarchies, i.e., directories containing subdirectories.

文件(或文件的链接)可以位于目录中。但是,更一般地说,目录可以包含文件列表或文件链接列表。在该定义中,术语“文件”包括目录是至关重要的。这允许存在目录层次结构,即包含子目录的目录。

From Wikipedia, ‘Computer file’

来自*,'计算机文件'

#3


1  

It is common to use a “file exists” function to check a path before writing to it. In this use case the type of file is irrelevant, if there is a directory called “/home/foo” you won't be able to create a file called “/home/foo”

在写入路径之前,通常使用“文件存在”功能来检查路径。在这个用例中,文件的类型是无关紧要的,如果有一个名为“/ home / foo”的目录,你将无法创建一个名为“/ home / foo”的文件。

Also PHP, one of the languages you mentioned, provides several functions depending on what kind(s) of file you care about:

此外,PHP(您提到的语言之一)提供了多种功能,具体取决于您关注的文件类型:

  • file_exists() will return TRUE for files, directories and symbolic links
  • 对于文件,目录和符号链接,file_exists()将返回TRUE

  • is_file() will return TRUE for files, but FALSE for directories and sym links
  • 对于文件,is_file()将返回TRUE,但对于目录和sym链接,则返回FALSE

  • is_dir() will return TRUE for directories, but FALSE for files and sym links
  • 对于目录,is_dir()将返回TRUE,对于文件和sym链接,则返回FALSE

  • is_link() will return TRUE for symbolic links, but FALSE for files and directories
  • 对于符号链接,is_link()将返回TRUE,对于文件和目录,则返回FALSE

#4


0  

Part of the Unix philosophy is that "everything is a file". This has influenced other environments as well to some degree.

Unix哲学的一部分是“一切都是文件”。这也在一定程度上影响了其他环境。

#1


3  

One reason is compatibility - anyone who has done 'check for existence' knows to exclude directories; changing that behaviour may confuse those who rely on that behaviour.

一个原因是兼容性 - 任何做过'检查存在'的人都知道要排除目录;改变这种行为可能会使那些依赖这种行为的人感到困惑。

Secondly, the underlying code often does a check on the operating system for existence in a catlog of filesystem entries - to the OS, a directory is the same as a file. In other words, it's looking for an entry of 'xyz' in the catalog not a file with name 'xyz' in the catalog.

其次,底层代码经常检查操作系统是否存在于文件系统条目的catlog中 - 对于OS,目录与文件相同。换句话说,它正在目录中寻找“xyz”条目,而不是目录中名为“xyz”的文件。

Backwards compatability is the main reason, I suspect.

我怀疑,向后兼容性是主要原因。

#2


2  

There is also a formal reason why a directory is a file:

目录是文件的正式原因如下:

Files (or links to files) can be located in directories. However, more generally, a directory can contain either a list of files or a list of links to files. Within this definition, it is of paramount importance that the term "file" includes directories. This permits the existence of directory hierarchies, i.e., directories containing subdirectories.

文件(或文件的链接)可以位于目录中。但是,更一般地说,目录可以包含文件列表或文件链接列表。在该定义中,术语“文件”包括目录是至关重要的。这允许存在目录层次结构,即包含子目录的目录。

From Wikipedia, ‘Computer file’

来自*,'计算机文件'

#3


1  

It is common to use a “file exists” function to check a path before writing to it. In this use case the type of file is irrelevant, if there is a directory called “/home/foo” you won't be able to create a file called “/home/foo”

在写入路径之前,通常使用“文件存在”功能来检查路径。在这个用例中,文件的类型是无关紧要的,如果有一个名为“/ home / foo”的目录,你将无法创建一个名为“/ home / foo”的文件。

Also PHP, one of the languages you mentioned, provides several functions depending on what kind(s) of file you care about:

此外,PHP(您提到的语言之一)提供了多种功能,具体取决于您关注的文件类型:

  • file_exists() will return TRUE for files, directories and symbolic links
  • 对于文件,目录和符号链接,file_exists()将返回TRUE

  • is_file() will return TRUE for files, but FALSE for directories and sym links
  • 对于文件,is_file()将返回TRUE,但对于目录和sym链接,则返回FALSE

  • is_dir() will return TRUE for directories, but FALSE for files and sym links
  • 对于目录,is_dir()将返回TRUE,对于文件和sym链接,则返回FALSE

  • is_link() will return TRUE for symbolic links, but FALSE for files and directories
  • 对于符号链接,is_link()将返回TRUE,对于文件和目录,则返回FALSE

#4


0  

Part of the Unix philosophy is that "everything is a file". This has influenced other environments as well to some degree.

Unix哲学的一部分是“一切都是文件”。这也在一定程度上影响了其他环境。