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.
views:
232answers:
3One 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.
Backwards compatability is the main reason, I suspect.
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”
Also PHP, one of the languages you mentioned, provides several functions depending on what kind(s) of file you care about:
file_exists()
will return TRUE for files, directories and symbolic linksis_file()
will return TRUE for files, but FALSE for directories and sym linksis_dir()
will return TRUE for directories, but FALSE for files and sym linksis_link()
will return TRUE for symbolic links, but FALSE for files and directories
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’