The files are created to keep the data organized. The files are designed in such a way that the data updation, retrieval, and collection becomes an easy task. In programming scenarios, file handling is an important aspect. Sometimes users need only file names, instead of the entire path. In such situations, certain methods can assist us in extracting the file names from the path.
This write-up will elaborate on different aspects of extracting file names from the path. The content of this post is organized as follows:
What is File Name and Path?
The file name is the actual name of the file created and the path is the exact location of the file. The filename can not be stored as a single value, it always needs a path or the location from where it can be easily retrieved. Let us understand it through an example,
“C:\\Python 1\\file1.txt” tells us that the C drive contains the folder Python 1 and the file “file1.txt” is created inside the folder. The combined form is referred to as “path”.
How to Extract/Get the File Name From the Complete Path?
To extract a file name from the path the required modules are imported, and certain built-in functions and methods are used in Python. These modules and built-in functions provide us with a way to fetch the file name when needed.
Below is the list of modules and built-in methods to extract the name of the file from the declared path.
- Extract File Name Using os.path.basename()
- Extract File Name Using the ntPath Module
- Extract File Name Using split()
- Extract File Name Using pathlib.path() Function
- Extract File Name Using Regular Expression
Method 1: Extract File Name Using os.path.basename()
The “os.path.basename()” is a method of the “OS” module that helps us extract the file/base name from the specified path. The practical execution of the basename() method is illustrated in the below code.
import os # Name of file with extension file_name = os.path.basename('C:\\Python 1\\file1.txt') # Print the name of the file print("The file name:" ,file_name)
In the above code:
- The os module is imported.
- In the next step, the “os.path.basename()” method is invoked to extract the file name from the specified path and store it in a variable named “file_name”.
- Finally, the extracted file name is printed on the console.
Method 2: Extract File Name Using the ntPath Module
In Python, the ntpath module can also be used to handle the paths of the files and folders. The ntpath.split() method splits the file path into two parts. The first part keeps the directory and the second comprises the file name. Below is the code implementation of the ntpath module in Python.
#Import the required module import ntpath #Create a function def method2(file_path): start, end= ntpath.split(file_path) return end or ntpath.basename(start) #Declare the filename with the path paths = ['C:\\Python 1\\exampleFile.txt'] print("The file name:" , [method2(file_path) for file_path in paths])
In the above-stated code:
- The ntpath module of Python is imported.
- The function is declared as “method2” and the file_path argument is passed to the function.
- The ntpath.split() breaks the entire path into two parts, the first part contains the directory and the sub-folder as “C:\\Python 1\\” and the second part contains the file name as “exampleFile.txt”.
- The print() function prints the filename that is declared in the specified path.
The file name that is “exampleFile.txt” is successfully extracted from the specified path.
Method 3: Extract File Name Using split()
This method breaks the entire path into the head and tail-like structure. The head contains the location with the folder and the tail contains the file name placed in the respective folder.
import os file = r'C:\\Python 1\\file1.txt' #split() method to break the path start, end= os.path.split(file) print("The File name Extracted from the path using split() method is: ", end)
In the code above,
- path.split() method is used to divide the given path.
- The print() function is utilized to show the extracted filename “file1.txt” from the given path.
The output below shows that the file name “file1.txt” is extracted from the path in Python
Method 4: Extract File Name Using pathlib.path() Function
The path() function extracts the file name from the path without any constraint of the path format. The code below implements the function to fetch the file name.
#Import the required module of Python from pathlib import Path #Declare the path of the file f_path= r'C:\\Python 1\\file1.txt' f_name = Path(f_path).name print("The name of the file is: ", f_name)
In the above Python code:
- The required module is imported.
- In the next step, the path of the file is declared.
- The .name() property retrieves the last element or the child element in the path.
- The print() function prints the file name.
Method 5: Extract File Name Using Regular Expression
Another convenient alternative to extracting file names from the path is by using regex. The regular expressions of Python use the following pattern [\w]+?(?=\.).
- The [\w] basically matches for the word.
- +? matches the string before “?”
- (?=\.) checks for all the given characters without a new line and stops at “.”
#Import the regex module import re path = 'C:\\Python 1\\file1.txt' patt = '[\w-]+?(?=\.)' # Search for the pattern that matches match = re.search(patt, path) # print the match using the group() function print("The File name extracted using regex is:", match.group())
In the above code,
- The regex module is imported and the path of the file with the regex pattern is specified.
- The search() function searches for the pattern in the path and the match.group() function returns the matching pattern.
The below output illustrates how you can extract the file name from the complete path using the regex.
This way users can extract file names from the path in Python using any of the above-discussed methods.
In Python, the filename from the path is extracted using different methods, built-in functions, and modules. The prominent methods include “os.path.basename()”, “split()”, and the pathlib.path() function. The “ntPath” module and “Regular Expressions” can also be implemented to extract/get the file name from the defined path. This write-up has demonstrated all these methods along with practical examples.