Python os.path.samestat()
Python os.path.samestat()方法用于检查给定的 stat 元组是否指向同一个文件。Stat元组是os.fstat()、os.lstat()或os.stat()方法返回的对象。它表示路径的状态。
语法: os.path.samestat(stat1, stat2)
参数:
stat1:统计元组。
stat2:统计元组。
返回类型:_此方法返回类bool的布尔值。如果两个stat元组引用同一个文件,此方法返回True,否则返回false。
示例1
使用os.path.samestat()方法检查给定的统计元组是否引用相同的文件。
# Python program to explain os.path.samestat() method
# importing os module
import os
# Path
path1 = "/home / ihritik / Desktop / file.txt"
# Get the status of
# the given path
# using os.stat() method
stat1 = os.stat(path1)
# Path
path1 = "/home / ihritik / Desktop / file.txt"
# open the file and get
# the file descriptor associated
# with it
fd = os.open(path1, os.O_RDONLY)
# Get the status of the
# file associated with the
# file descriptor fd
# using os.fstat() method
stat2 = os.fstat(fd)
# Check whether the
# stat1 and stat2 refer
# to the same file
sameFile = os.path.samestat(stat1, stat2)
# Print the result
print(sameFile)
os.close(fd)
# Path
path3 = "/home / ihritik / Desktop / file.txt"
# Get the status of
# the above path
# using os.lstat() method
stat3 = os.lstat(path3)
# Check whether the
# stat1 and stat3 refer
# to the same file
sameFile = os.path.samestat(stat1, stat3)
# Print the result
print(sameFile)
# Path
path4 = "/home / ihritik / Documents / file.txt"
# Get the status of
# the above path
# using os.stat() method
stat4 = os.stat(path4)
# Check whether the
# stat1 and stat4 refer
# to the same file
sameFile = os.path.samestat(stat1, stat4)
# Print the result
print(sameFile)
输出:
True
True
False