用python在windows中获取文件ID?

时间:2018-01-24 07:00:35

标签: python windows file

您好我想通过python从Windows上的文件中获取文件ID。当我搜索时,我只能找到如何用其他语言进行搜索。有谁知道我如何在python中实现这个目标?

2 个答案:

答案 0 :(得分:0)

据我观察和研究,没有这样的文件ID 可用。但是,您可以在 Windows Mac 上创建创建日期,并在上次修改的Linux 。这两个通常足以找到唯一的文件,即使它们被重命名,更改或其他任何文件。

以下是source SO thread I found the solution

的操作方法
import os
import platform

def creation_date(path_to_file):
    """
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See http://stackoverflow.com/a/39501288/1709587 for explanation.
    """
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime

答案 1 :(得分:0)

import os

path_to_file = r"path_to_your_file"
file_id = os.stat(path_to_file, follow_symlinks=False).st_ino
print(hex(file_id))

从命令行检查结果:

c:\> fsutil file queryfileid path_to_your_file

因此在Python中,您也可以使用

print(os.popen(fr"fsutil file queryfileid path_to_your_file").read())

或当您有硬链接时:

print(os.popen(fr"fsutil hardlink list path_to_your_file").read())

查找具有ID的文件名:

print(os.popen(fr'fsutil file queryFileNameById c:\ the_file_id').read())