Open In App

Python – Get file id of windows file

Last Updated : 23 Jul, 2021
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.
     


Previous Article
Next Article

Similar Reads

Python Program to Get the File Name From the File Path
In this article, we will be looking at the program to get the file name from the given file path in the Python programming language. Sometimes during automation, we might need the file name extracted from the file path. Better to have knowledge of: Python OS-modulePython path moduleRegular expressionsBuilt in rsplit()Method 1: Python OS-moduleExamp
4 min read
How To Get The Uncompressed And Compressed File Size Of A File In Python
We are given a compressed file as well as uncompressed file. Our task is to get the size of uncompressed as well as compressed file in Python. In this article, we will see how we can get the uncompressed and compressed file size of a file in Python. What is Uncompressed And Compressed File?Uncompressed File: An uncompressed file is a file that has
3 min read
Get a List of Installed Softwares in Windows using Python
In this article, we are going to write a Python script to get the installed software list in windows. We will use the subprocess module to interact with cmd and to retrieve information into your Python IDE. We can read the cmd command through the subprocess module. Let's see the logic, if we run this wmic product get name code into our terminal the
1 min read
How to get Scrapy Output File in XML File?
Prerequisite: Implementing Web Scraping in Python with Scrapy Scrapy provides a fast and efficient method to scrape a website. Web Scraping is used to extract the data from websites. In Scrapy we create a spider and then use it to crawl a website. In this article, we are going to extract population by country data from worldometers website. Let's i
2 min read
Python - Get number of characters, words, spaces and lines in a file
Given a text file fname, the task is to count the total number of characters, words, spaces, and lines in the file. Python - Get number of characters, words, spaces and lines in a fileAs we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to calculate the total number of characters, words
5 min read
How to get file size in Python?
We can follow different approaches to get the file size in Python. It's important to get the file size in Python to monitor file size or in case of ordering files in the directory according to file size. Method 1: Using getsize function of os.path module This function takes a file path as an argument and it returns the file size (bytes). Example: C
3 min read
Get sorted file names from a directory by creation date in Python
In this article, we will understand how to retrieve sorted file names from a directory using Python. For this, we would make use of the Python glob library's glob function. There is no need to install this module externally because it is already included with Python. Firstly, The glob function would be used to obtain the list of files/directories w
3 min read
How to get file extension in Python?
In this article, we will cover How to extract file extensions using Python. How to Get File Extension in Python? Get File Extension in Python we can use either of the two different approaches discussed below: Use the os.path Module to Extract Extension From File in PythonUse the pathlib Module to Extract Extension From File in PythonMethod 1: Using
2 min read
How to get the permission mask of a file in Python
In UNIX-like operating systems, new files are created with a default set of permissions. We can restrict or provide any specific set of permissions by applying a permission mask. Using Python, we can get or set the file's permission mask. In this article, we will discuss how to get the permission mask of a file in Python. Get the Permission Mask of
2 min read
Get File Size in Bytes, Kb, Mb, And Gb using Python
Handling file sizes in Python is a common task, especially when working with data processing, file management, or simply understanding resource usage. Fortunately, Python provides several methods to obtain file sizes in various units, such as bytes, kilobytes, megabytes, and gigabytes. In this article, we will explore five simple and generally used
2 min read
three90RightbarBannerImg