我正在嘗試獲取NTFS對象ID以在Python備份程序中使用。我在我的頭上,但設法創建一個函數返回...什麼。從Python中調用FSCTL_CREATE_OR_GET_OBJECT_ID
import sys
import win32file
import winioctlcon
def object_id(filename):
"""
NTFS OBJECT_ID
"""
fhandle = win32file.CreateFileW(
# FileName
filename,
# DesiredAccess
win32file.GENERIC_READ,
# ShareMode
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
# SecurityAttributes
None,
# CreationDisposition
win32file.OPEN_EXISTING,
# FlagsAndAttributes
0
)
obj_id = win32file.DeviceIoControl(
# Device : PyHANDLE
# Handle to a file, device, or volume
fhandle,
# IoControlCode : int
# IOControl Code to use, from winioctlcon
winioctlcon.FSCTL_CREATE_OR_GET_OBJECT_ID,
# InBuffer : str/buffer
# The input data for the operation, can be None for some operations.
None,
# OutBuffer : int/buffer
# Size of the buffer to allocate for output, or a writeable buffer as
# returned by win32file::AllocateReadBuffer.
64,
# Overlapped=None : PyOVERLAPPED An overlapped object for async
# operations. Device handle must have been opened with
# FILE_FLAG_OVERLAPPED.
None
)
fhandle.Close()
return obj_id
從調用此函數的幾個輸出樣本就像一個海峽,「↑·∟âkòπ◄êδ%dΘπ╧hMêc▌Æ╧J¿/╧y╠┘ôπ↑·∟âkòπ◄êδ%dΘπ╧ 」。對於我的程序而言,只要它與我備份的每個文件保持一致,那就沒有問題。但我在這裏做了什麼可怕的錯誤?理想情況下,我想盡可能正確地實現這一點。
你是如何從上面給出的(看起來很瘋狂的)str值轉換成這三個字母數字?! –
只需從十六進制字符串中取出即可。 – user2120666
你能展示一個將我的字符串轉換爲你的十六進制字符串的Python函數嗎?我一直在努力整晚複製你的結果! –