Python get file name without extension from path
This tutorial will demonstrate various ways to get the file name without extension from a file path in Python. Suppose our goal is to get the file name from a list of file paths that are in the form of strings, such as from the path Desktop/folder/myfile.txt
, we get only .txt
the file name without the extension myfile
.
pathlib.path().stem
Get the file name without extension from path
using method in Python
path().stem
The method takes a file path as input and returns the file name by extracting it from the file path. For example, from the path Desktop/folder/myfile.txt
, it will return the file without .txt
the extension myfile
.
The following code example demonstrates how to use path().stem
to get the file name without the file extension from a file path.
from pathlib import Path
file_path = "Desktop/folder/myfile.txt"
file_name = Path(file_path).stem
print(file_name)
Output:
myfile
Get the file name without extension from a path using the os.path.splitext()
and methods
in Pythonstring.split()
os
The module's path.splitext()
method takes a file path as a string input and returns the file path and file extension as output.
Since we want to get the file name from the file path, we can first use os.path.splitext()
the method to remove the file extension from the file path. The first element of the split result is the file path without the extension. This result is /
further split using as a separator. The last element is the file name without the extension. The following code example demonstrates how to use the path.splitext()
and string.split()
methods to get the file name without the extension from the file path.
import os
file_path = "Desktop/folder/myfile.txt"
file_path = os.path.splitext(file_path)[0]
file_name = file_path.split("/")[-1]
print(file_name)
Output:
test
Get the file name from the path using the os.path.basename()
and methods
in Pythonos.path.splitext()
In Python, os
the module's path.basename()
basename method takes a file path as input and returns the basename extracted from the file path. For example, Desktop/folder/myfile.txt
the basename of is myfile.txt
.
Since we want to get the file name from the file path, we can use path.basename()
the method to extract the base name and use to path.splitext()
extract the file name. The following code example demonstrates how to use the path.basename()
and path.splitext()
methods to get the file name from a file path.
import os
file_path = "Desktop/folder/myfile.txt"
basename = os.path.basename(file_path)
file_name = os.path.splitext(basename)[0]
print(file_name)
Output:
myfile
Assume that we need to Desktop/folder/myfile.tar.gz
get the file name without .
the part after from the path , such as myfile
instead of myfile.tar
, we can use string.index()
the method myfile.tar
to extract only from myfile
. However, the disadvantage of this method is that if .
is part of the file name, for example my.file.tar.gz
, it will return my
as the file name.
Below is an example of the code, how we can use remove from string.index()
the output of the method explained above .myfile.tar
.tar
file_name = "myfile.tar"
index = file_name.index(".")
file_name = file_name[:index]
print(file_name)
file_name = "my.file.tar"
index = file_name.index(".")
file_name = file_name[:index]
print(file_name)
Output:
myfile
my
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Read the first line of a file in Python
Publish Date:2025/05/05 Views:192 Category:Python
-
In Python, we have built-in functions to handle different file operations. A text file contains a sequence of strings where each line \n is terminated by a newline character. In this tutorial, we will learn how to read the first line of a t
Calculate the time difference between two time strings in Python
Publish Date:2025/05/05 Views:182 Category:Python
-
Sometimes we have to deal with date and time related problems in programming. In Python, date and time are not data types themselves. Despite this, Python provides a large number of functions and libraries to help deal with such problems. O
Optional parameters in Python
Publish Date:2025/05/05 Views:151 Category:Python
-
In Python, there is something called default arguments. It is also called optional parameters or optional arguments in python. Parameters refer to the inputs of a function. Functions with multiple optional parameters in Python Whenever you
Writing floating point numbers to a file in Python
Publish Date:2025/05/05 Views:91 Category:Python
-
Python makes writing data to a file a seamless task. The data is written to the file in the form of strings. In this article, you will learn how to write float values to a file in Python. Writing floating point numbers to a file in Py
How to read specific lines from a file in Python
Publish Date:2025/05/05 Views:181 Category:Python
-
A common way to read files in Python is to read the file completely and then process specific lines. Reading files in Python is fast, for example, it takes about 0.67 seconds to write a 100MiB file. However, if the file size exceeds 100MB,
Calculating the arithmetic mean in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The term arithmetic mean is the average of the numbers. The mathematical formula to determine the arithmetic mean is to divide the sum of the numbers by the count. It is determined in Python in the following way. Use mathematical formulas.
Cosine Similarity in Python
Publish Date:2025/05/05 Views:81 Category:Python
-
Cosine similarity measures the similarity between two lists of vectors by calculating the cosine angle between them. If you consider the cosine function, it has a value of 1 at 0 degrees and -1 at 180 degrees. This means that for two overla
Exponential and Logarithmic Curve Fitting in Python
Publish Date:2025/05/05 Views:79 Category:Python
-
Curve fitting is a very effective tool that is widely used in analysis. Curve fitting method studies the relationship between independent variables (also called predictor variables) and dependent variable (called response variable). This me
Calculating Slope in Python
Publish Date:2025/05/05 Views:60 Category:Python
-
In mathematics, the slope of a given line is the value at which its steepness is calculated. It also helps to characterize the direction of a given line. The extent of a line can also be calculated using Python programming language. This ar