I would like to call the method win32file.OpenFileById
to get a file by its object ID using Python. This reference mentions it using C++: https://devblogs.microsoft.com/oldnewthing/20110228-00/?p=11363 but I want to do it using Python.
I read this reference: https://timgolden.me.uk/pywin32-docs/win32file__OpenFileById_meth.html and I tried the following:
def get_file_from_file_id(volume: str, file_id):
if not volume.endswith(":"):
volume += ":"
path = os.path.join(volume, ".can_delete")
print(path)
if not os.path.exists(path):
with open(path, "w") as volume_file:
volume_file.write("You can delete this file.")
with open(path, "r") as volume_file:
volume_file_handle_num = msvcrt.get_osfhandle(volume_file.fileno())
handle_num = win32file.OpenFileById(
volume_file_handle_num,
file_id,
win32file.GENERIC_READ,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE | win32file.FILE_SHARE_DELETE,
0,
None
)
path = win32file.GetFinalPathNameByHandle(handle_num)
return path
but I always get this error:
handle_num = win32file.OpenFileById(
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: FileId must be an integer or GUID
The object id cannot be integer because it contains letters. I could not find documentation about how to get the GUID type.
Mubarak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.