In , asvetlov suggests adding an implementation of os.sameopenfile to ntpath.py: --- Jason, as I see you implemented os.samefile (actually ntpath.samefile) but os.sameopenfile is still not implemented. Looks like it's easy to do: while GetFinalPathNameByHandle already accept file handle you can use CRT function _get_osfhandle(fd) to convert file descriptors (arguments for sameopenfile) to file handles and pass it to GetFinalPathNameByHandle. For me samefile and sameopenfile has very close semantic and I like to see both if it's possible. --- This seems entirely reasonable, but I would like to track and tackle this as a separate issue.
As NTFS supports hard links, I think GetFileInformationByHandle should be used to compare handles (looking at dwVolumeSerialNumber, nFileIndexHigh,nFileIndexLow)
Here's a patch implementing ntpath.sameopenfile with _getfileinformation in Modules/posixmodule.c. Martin's suggestion is the best way of doing this, which was the basis for _getfileinformation implementation. It returns a tuple of the three relevant fields, and ntpath.sameopenfile compares the results of those tuples for two files and returns the result.