I have a ton of folders nested inside one another.
What is time complexity of Python's os.path.exists() ?
Does it change if used with different OS ?
Asked
Active
Viewed 2,476 times
-2

garywinthorpe
- 42
- 7
-
2[Quora answer](https://www.quora.com/Is-the-time-complexity-for-finding-a-directory-entry-in-one-directory-of-Ext4-file-system-O-n-where-n-is-the-number-of-existing-directory-entries) for `ext4` - the data structure might be different for other systems. – meowgoesthedog Aug 24 '18 at 08:12
-
underlying implementation [seems to call `os.lstat`](https://github.com/python/cpython/blob/3.7/Lib/posixpath.py). It seems it doesn't follow sym links so it's probably a little more optimized. – aug Aug 24 '18 at 08:16
-
I came here wondering how os.path.exists() would behave with a remotely mounted CIFS filesystem. Hence the mark as duplicate only taking into account ext4 and the current answers are very disappointing. – kriss Dec 24 '21 at 09:40
2 Answers
2
os.path.exists
just performs a system call and returns True if the path points to an existing file or directory.
Python seems to performa an lstat()
system call on the given path.
If the operation only consists in a lookup in a hash table, then the cost is O(1)
, but it may depend on the Operating System and in how is it implemented internally.

abc
- 11,579
- 2
- 26
- 51
-
actually, path.exists uses os.stat, but that's just a minor detail. https://github.com/python/cpython/blob/master/Lib/genericpath.py#L16 – Jblasco Aug 24 '18 at 08:27
-
1The answer is referring to the underlying C function being `lstat`. – Sam Brightman Nov 05 '19 at 11:46
0
Given you are asking the OS if a single file exists, it does not need to do any algorithmic logic, or go down your path... I don't see how it could be anything else than O(1).

Jblasco
- 3,827
- 22
- 25
-
So, if I do something like `os.path.exists("/home/myProject/testFolder/")`, it should be O(1) ? – garywinthorpe Aug 24 '18 at 08:23
-
@garywinthorpe, see https://stackoverflow.com/questions/6176547/python-complexity-of-os-path-exists-with-a-ext4-filesystem – Jblasco Aug 24 '18 at 08:32
-
If the underlying kernel call needs to list all files in some directory or iterate through directory blocks it may depends on the number of files in the pointed directory. For instance it was the implementation of old VFAT. With ext4 it seems to be O(1) – kriss Dec 24 '21 at 09:37
-
Yes, you are right, @kriss. That's what is mentioned in the link I posted in the comment. – Jblasco Jan 03 '22 at 13:00