5

Is it possible for a file f that os.path.isfile(f) and os.path.isdir(f) both evaluate to false?

What would a third category be named in this case?

Szenario: I have created a contentless file using touch on ubuntu 18.04, which definitely is not a directory. Python (version 3.5) nevertheless returns false on isfile(f).

Edit: It turns out that the file indeed returned True, the earlier output was a small mistake in my code.

The question still remains as I thought about it this way:

  1. I have a set (Set A) of all files in a directory.

  2. I create a subset via isfile filtering (Set B).

  3. I create a subsets via isdir filtering (Set C).

    Would the union of B and C be equal to A?

Community
  • 1
  • 1
René Jahn
  • 1,155
  • 1
  • 10
  • 27
  • 4
    try it on a symlink. or on a device file. or a socket. or something other than a file or directory you find on your filesystem. but if `f` is a file as you state, `isfile` will return `True`. – hiro protagonist Oct 04 '18 at 05:45
  • "file f" - that would make `isfile` always true and `isdir` always false. – Sayse Oct 04 '18 at 05:45
  • @U9-Forward Dude, all I want to know is specified in the question. – René Jahn Oct 04 '18 at 05:51
  • To clarify, does the file in question definitely exist? Can you please add example code of how exactly you produce this case? – Lomtrur Oct 04 '18 at 05:58
  • @Lomtrur file has been created with touch (added the szenario above) – René Jahn Oct 04 '18 at 05:59
  • 7
    I think this is a valid question. As I read it, Minato does not try to make it return `False`. OP just wants to know if you have to check for different possibilities in your code. @U9-Forward, your comments seem unneccessary aggressive and abusive. – Christian König Oct 04 '18 at 05:59
  • @Minato: the `f` in your example, is it a string? what does `type(f)` say? – return42 Oct 04 '18 at 06:12
  • This is a pretty clear question and should not be closed as unclear. What it *should* be closed as is a duplicate of https://stackoverflow.com/questions/32741079/do-file-systems-have-other-components-rather-than-files-and-directories – user2357112 Oct 04 '18 at 18:08
  • 1
    @ChristianKönig Sorry, i removed all my comments, i didn't see all the edits, and i undownvoted, and upvoted your comment – U13-Forward Oct 05 '18 at 01:12
  • @ChristianKönig And voted to reopen.. – U13-Forward Oct 05 '18 at 01:12
  • @ChristianKönig And upvoted the question – U13-Forward Oct 05 '18 at 01:13

1 Answers1

3

Is it possible for a file f that os.path.isfile(f) and os.path.isdir(f) both evaluate to false?

yes .. if you ask for existence os.path.exists(f).

EDIT: to answer your more detalied question ..

Would the union of B and C be equal to A?

Normaly yes .. if you can assert that none of them is deleted in the meantime.

Take into account that dead links are not exists, which means that os.path.exists(deadlink) of a (existing) link pointing to a dead end results in False while os.path.islink(deadlink) results in True, no matter if it points to a existing object or a dead end.

return42
  • 543
  • 3
  • 10
  • [There is also at least one bug that can cause this behaviour on Windows.](https://bugs.python.org/issue33105) I haven't found anything for Ubuntu. – Lomtrur Oct 04 '18 at 06:10