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.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.