Open In App

Python – Get file id of windows file

Improve
Improve
Like Article
Like
Save
Share
Report

File ID is a unique file identifier used on windows to identify a unique file on a Volume. File Id works similar in spirit to a inode number found in *nix Distributions. Such that a fileId could be used to uniquely identify a file in a volume. 
We would be using an command found in Windows Command Processor cmd.exe to find the fileid of a file. In order to access/invoke cmd.exe from python, we would be using popen() function found in the *os library. 
os library is preinstalled into most python distributions. If not, one can install the library by running the following command in the command processor of their operating system: 
 

pip install os

* It is not necessary to use only os library for the purpose of invoking the command line. One could also use alternatives for invoking the commandline (ex. subprocess.popen() could also be used for the purpose).
In this article, we will query fileId of a file on our drive, by using its path. And then later would use this fileId to obtain the absolute path to the file. 
 

Python3




# importing popen from the os library
from os import popen
 
# Path to the file whose id we would
# be obtaining (relative / absolute)
file = r"C:\Users\Grandmaster\Desktop\testing.py"
 
# Running the command for obtaining the fileid,
# and saving the output of the command
output = popen(fr"fsutil file queryfileid {file}").read()
 
# printing the output of the previous command
print(output)


Output: 
 

File ID is 0x00000000000000000001000000000589

Now we would be using the fileid of this file to obtain the path to the file in the volume. 
 

Python3




from os import popen
 
# Fileid of the file
fileid = "0x00000000000000000001000000000589"
 
# Running the command for obtaining the file path,
# of the file associated with the fileid
output = popen(fr"fsutil file queryfilenamebyid C:\ {fileid}").read()
 
print(output)


Output: 
 

A random link name to this file is \\?\C:\Users\Grandmaster\Desktop\testing.py

Where C:\Users\Grandmaster\Desktop\testing.py is the path to the file
Explanation:
The core of both the programs lies in the statements popen(fr”fsutil file queryfileid {file}”) and popen(fr”fsutil file querfyfilenamebyid C:\ {fileid}”). The explanation of which is:- 
 

  • The term fsutil in the command, is a command found in Windows command processor, used to perform File and Volume specific commands, Hardlink management, USN journal management, Object ID and Reparse point management. 
     
  • The first argument (to fsutil) file is used to get access to file specific options in the file system. Other arguments exists such as USN for performing operations on the USN Journal, hardlink for performing operations on hard links of the filesystem. 
     
  • The second argument queryfileid is used to obtain the fileId of the pathname provided in the fourth argument. If this file is not found in the path provided as the fourth argument, an negative message Error: The System cannot find the file specified. is returned. If the file exists at the path provided, then the command returns the fileId of the file.
     
  • In the second code the second argument is queryfilenamebyid which works in the opposite way to queryfileid. It takes the Volume as the third argument where the file associated with the fileId is located. As the fourth argument it takes the fileId and returns the path, where the file with that fileId is located in that volume.
     


Last Updated : 23 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads