6 Best Ways to Get Filename Without Extension in Python

Files are used for storing information with the ability to read and write on them. The operations which can be performed on files in python are – read, write, open, close, rename and delete. With the help of files, we can store information inside the computer’s storage. Each file has a pathname that tells about the location at which the file is stored. The pathname consists of information about the directory where the file is stored, the name of the file, and the extension with which the file is stored. In this article, we shall be looking at six different ways in python to get filename without extension.

How to get the filename?

As mentioned before, a pathname consists of three parts – the extension of the file, the filename, and the file’s location. First, we will have to separate the pathname and the extension. Then from the pathname, we shall separate the filename with the directory path. We shall be looking at 6 ways in python to get filename without extension.

  1. Using splitext()
  2. With the split() function
  3. Using rfind()
  4. Basename() function
  5. Using pathlib.Path.stem()
  6. By the rpartition() function

Also, Read | Apex Ways to Get Filename From Path in Python

1. Using splitext() to Get Filename Without Extension in Python

The splitext() method can be used to get filename in python without extension. The method is present in the os module of python. Using os.path module, we can use it to work with pathnames in python. With splitext(), we can split the entire pathname into two parts – the extension and the root.

The syntax of splitext() method is:

os.path.splitext(path)

The function takes the pathname as an argument and returns a tuple containing the separated extension and root names.

Let us implement the function in python. First, we shall import the os module.

import os

We have a variable named ‘directory’ which contains the complete path of out file.

directory = '/Users/Programs/Directory/program1.csv'

Now, we will call the splitext() method by os.path.splitext(). We shall pass the variable ‘directory’ inside the splitext() method. Since the method generates a tuple containing two parts, we shall save the extension into a variable named ‘extension’ and the rest of the path into a variable named ‘pathname’.

pathname, extension = os.path.splitext(directory)

If we try to print the output of os.path.splitext(), the tuple returned would be:

('/Users/Programs/Directory/program1', '.csv')

Now, we shall split the variable ‘path’ with the forward slash as the separator.

filename = pathname.split('/')

After that, we shall print the last item of the list ‘filename’, which will be the actual filename.

print(filename[-1])

The output is:

program1

The entire code is:

import os
 
directory = '/Users/Programs/Directory/program1.csv'

pathname, extension = os.path.splitext(directory)
filename = pathname.split('/')

print(filename[-1])

Here, if you want the complete pathname, you can simply skip splitting the variable ‘pathname’ and directly have it as the filename.

2. With the split() method to Get Filename Without Extension in Python

Similar to the splitext() method, we can also use the split() method to get filename without extension. For using the split() function, there is no need to import the os module. We will have to call the split() function twice.

First, we shall split the extension and the rest of the pathname. Then, we shall split the extension of the file. The separator for the first split() function will be the ‘.’ character and the separator for the second split() function will be the forward-slash ‘/’.

Here, after the first splitting, we will store the output into variable ‘name’. Then we shall split the first item of the list ‘name’ by using ‘name[0].split()’ with forward slash as the separator. Then we will print the last item of the list ‘filename’.

directory = '/Users/Programs/Directory/program1.csv'

name = directory.split('.')
filename = name[0].split('/')

print(filename[-1])

The output is:

program1

Here, if you want the complete pathname, we will simply print ‘name[0]’.

3. Using rfind() to Get Filename Without Extension in Python

We can also use the rfind() method to split the filename to separate the pathname and the extension. The function rfind() will find the last occurrence of the given value.

The syntax of the rfind() function is:

string.rfind(value, start, end)

We can all the rfind() method with a string. Here, a value is an item whose last occurrence has to be returned. The start and end represent the starting and ending positions while searching the string. By default, the start value is 0, and the end value is the length of the string.

Here, we will call the rfind() function using directory.rfind(). Inside the rfind() function, we shall pass the dot ‘.’ as the value. We shall save the index of the dot character into a variable named ‘index’. Then, we shall print the string ‘directory’ from the 0th character to the value of ‘index’.

directory = '/Users/Programs/Directory/program1.csv'
index = directory.rfind(".")
print(directory[:index])

The output is:

/Users/Programs/Directory/program1

If you only want the filename ‘program1’, then we can split() using the forward-slash character.

4. Using Basename() function to Get Filename Without Extension in Python

We can also use the basename() function from the os module to separate the filename. With the basename() function, we can get the base name of a file from the entire directory name.

The syntax of the function is:

os.path.basename(path)

We have to pass the entire pathname into the basename() function as an argument. First, we will import the os module.

The output of ‘os.path.basename(directory)’ will be ‘program1.csv’. So, we will call the split function and pass the dot character as the separator. That will return a list containing [ ‘program1’ , ‘csv’ ]. So we will print the first item of that list.

import os
directory = '/Users/Programs/Directory/program1.csv'
print(os.path.basename(directory).split('.')[0])

The output will be:

program1

5. Using pathlib.Path.stem() to Get Filename Without Extension in Python

The pathlib module in python is used to deal with the file paths. When we don’t want to get the complete path, we can use pathlib.Path.stem(). Using the stem property, we will get the file name without its extension.

For that, first, we will have to import the pathlib module. Then we shall pass the ‘directory’ inside the pathlib.Path() function. Then, we shall use the stem property.

import pathlib

directory = '/Users/Programs/Directory/program1.csv'
filename = pathlib.Path(directory).stem

print(filename)

The output filename is:

program1

6. By the rpartition() function to Get Filename Without Extension in Python

The rpartition() function splits a given string into three parts. One part will be the separator and the other two parts will be the strings to the left and the right side of the separator.

The syntax of the rpartition() function is:

string.rpartition(separator)
directory = '/Users/Programs/Directory/program1.csv'
print(directory.rpartition('.')[0])

Output:

/Users/Programs/Directory/program1

That is all for 6 ways in Python to Get Filename Without Extension. If you have any questions, let us know in the comments below.

Until next time, Keep Learning! 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments