Python| os.path.samestat() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path模块是Python中OS 模块的子模块,用于常见的路径名操作。
Python中的os.path.samestat()方法用于检查给定的stat元组是否引用同一个文件。 Stat 元组是一个对象,它可能已经由 os.fstat()、os.lstat() 或 os.stat() 方法返回。它表示路径的状态。
Syntax: os.path.samestat(stat1, stat2)
Parameter:
stat1: A stat tuple.
stat2: A stat tuple.
Return Type: This method returns a Boolean value of class bool. This method returns True if both stat tuple refer to the same file otherwise returns false.
代码:使用 os.path.samestat() 方法检查给定的统计元组是否引用同一个文件。
Python3
# 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
参考: https://docs。 Python.org/3/library/os.path.html