Python Glossary Русский

all Python 3osfunction

os.path.samestat

os.path — metadata (stat)

Check whether two os.stat_result objects describe the same file.

Syntax

os.path.samestat(s1, s2)

It compares existing os.stat results by device number and inode. Use it when stat has already been called and you want to avoid hitting the disk again, e.g. walking a tree while guarding against loops. A stat result is a snapshot: if the file was replaced since, the comparison will not show it.

Examples

import os
s = os.stat(os.__file__)
print(os.path.samestat(s, s))   # → True
s2 = os.stat(os.__file__)
print(os.path.samestat(s, s2))   # → True (два независимых stat одного файла)
print((s.st_dev, s.st_ino) == (s2.st_dev, s2.st_ino))   # → True (samestat сравнивает ровно эту пару полей)
d = os.stat(os.path.dirname(os.__file__))
print(os.path.samestat(s, d))   # → False
print(os.path.samestat(os.__file__, os.__file__))   # → AttributeError (нужны stat_result, а не строки-пути)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy