3

I have seen this python snippet in a video tutorial which checks if the listed item is a directory or a file:

for item in os.listdir("."):
    if os.path.isfile(item):
        # do something
    elif os.path.isdir(item):
        # do somethin
    else:
        # What is this case ?!

is it possible that the else statement could be hit?

3bdalla
  • 407
  • 1
  • 10
  • 28
  • 2
    There's also `islink`, but [the docs](https://docs.python.org/2/library/os.path.html#os.path.isfile) state that `isfile` and `isdir` follow links anyway, and `ismount`. – jonrsharpe Sep 23 '15 at 13:44

3 Answers3

4

As @sisoft says, the simple answer is yes: there do exist file systems that support file types other than files and directories.

The longer answer, if you're interested, is that the types supported by a file system vary wildly with the file system. UNIX treats a huge number of things as a 'file' (meaning an object in the file system) and so has many types. Windows has a more restricted set of objects (files, directories and links only I believe (no source))

The POSIX specification (implemented by many file systems) for a file system doesn't specify what objects it must support(source).

Generally, file system is a fairly open term that can refer to any object store. The objects that it stores could be anything.

If you'd like to learn more about file systems, there is a great chapter in Operating Systems which gives an easily accessible introduction.

Community
  • 1
  • 1
Tom Rees
  • 455
  • 2
  • 10
2

Yes. There are other types, like pipes, sockets, device nodes. For example isfile() and isdir() returns False for most files from /dev.

You can see https://en.wikipedia.org/wiki/Unix_file_types at first.

sisoft
  • 953
  • 10
  • 22
  • Yes exactly. I remember that I did by mistake `vi /dev/sda` and the vi prompt said `/dev/sda is not a file` – 3bdalla Sep 23 '15 at 13:48
  • Are you talking about the filesystem entry types or about generic file types? Cause the answer depends on what is meant by the filesystem - is it something that starts with / on Unix or it is a particular filesystem as a data layout (say ex3 or NTFS). – Eugene Mayevski 'Callback Sep 23 '15 at 17:53
2

is it possible that the else statement could be hit?

Your code fragment uses a narrow definition of files and directories: os.stat(path) (follows symlinks) is successful and either S_ISREG or S_ISDIR are true correspondingly.

else clause may be triggered for non-existing paths or due to permission errors for regular files and directories.

POSIX defines the following marcos:

S_ISBLK(m)
Test for a block special file.
S_ISCHR(m)
Test for a character special file.
S_ISDIR(m)
Test for a directory.
S_ISFIFO(m)
Test for a pipe or FIFO special file.
S_ISREG(m)
Test for a regular file.
S_ISLNK(m)
Test for a symbolic link.
S_ISSOCK(m)
Test for a socket.

i.e., in addition to a regular file and a directory, there could be sockets, symlinks, pipes, block/character devices:

>>> import os
>>> import stat
>>> stat.S_ISBLK(os.stat('/dev/sda').st_mode)
True

There could exist other objects that have meaning only for a particular filesystem.

jfs
  • 399,953
  • 195
  • 994
  • 1,670