Let’s say you did a search for files matching a certain pattern in a directory using :
import glob filePaths = glob.glob("C:\\Temp\\*.txt")print filePathsThis will list the full file paths with a .txt extension in the C:\Temp directory. For example: C:\\Temp\\test.txt.
But if you wanted to get just the file name, how would you go about that? It took me a little while to find an answer, and the method not super obvious, so I’ll post it here.
import glob, os filePaths = glob.glob("C:\\Temp\\*.txt") for filePath in filePaths: print os.path.basename(filePath)