Get the file id of windows file in Python

Hello everyone, in this tutorial, we will be learning how to get the file id of windows file in Python. A file id is an identifier that uniquely identifies a file on a Volume on windows.

In order to get the file id, we will be using one of the commands found in the windows command processor. We will execute the command using popen() function in the os library.

The command that we will be using to get the file id is as follows:

fsutil file queryFileId file_name

Let us try to understand this.

fsutil command is used for commands related to files or volumes, or Hardlink Management, USN Journal Management, Reparse Point Management, etc. The term file in the command is used to select the file specific option. Then queryFileId is used to get the fourth argument as the path of the file for which we want the file id. The fourth argument is the name/path of the file.

Have a look a the following code.

import os

file_id = os.popen(fr'fsutil file queryFileId codespeedy.txt').read()



File ID is 0x0000000000000000002f00000003ecc4

We can also use the subprocess module to do the above. See the below code and its output.

import subprocess

subprocess.Popen(fr'fsutil file queryFileId codespeedy.txt').communicate()


File ID is 0x0000000000000000002f00000003ecc4

We can also get the file name from a given file id using the following command. Have a look.

fsutil file queryFileNameById C:\ file_id

And here is the code.

import os

file_name = os.popen(fr'fsutil file queryFileNameById c:\ 0x0000000000000000002f00000003ecc4').read()



A random link name to this file is \\?\C:\Users\Ranjeet Verma\Desktop\codespeedy.txt

You can also use to get the same.

Thank you.

Also read: Python File Handling

Leave a Reply

Your email address will not be published. Required fields are marked *