I am making a Plugin and I uploaded my shp file using my shp file path, how can I extract the name of the file from it?

Example: D:/Qgis files/sub_folderName/FileName.shp

I want to save the FileName in a string as FileName using PyQGIS.

  • 4
    It is a pure Python problem without geospatial component
    – gene
    Commented Jun 12, 2021 at 8:24

1 Answer 1


Utilizing the "os" library of python will provide the solution to this problem. You can use the following command to extract the filename from the complete path:

import os
os.path.basename("Write the path here").split(".")[0]

os.path.basename() extracts the filename from the given path. So, in your example case, it would be FileName.shp. Then, I split this FileName.shp with "." using the split function. So, the split function will return a list with the filename and its extension. Then, I used indexing "[0]" to extract the filename from that list. Hope I'm able to explain it properly.


Not the answer you're looking for? Browse other questions tagged or ask your own question.