Python get filename from the path

In this Python tutorial, we will discuss how to get filename from the path, and also we will see how to get the file size in python.

  • Python get the filename from the path
  • Python get the file size
  • Python get file extension from filename
  • Python get filename without extension

Python get filename from the path

To get the filename from a path in Python, we need to import os and then the path is added.

Example:

import os
print()
print(os.path.basename('E:\project-python\string\list.py'))
print()

After writing the above code (python get filename from the path), Ones you will print then the output will appear as a “ list.py ”. You can refer to the below screenshot for creating a python get filename from the path.

Python get filename from the path
Get filename from the path in Python

Python get the file size

In python, to get the file size we will use the os module and the python os module has getsize() function where the file name is passed as an argument and return the size of a file in bytes.

import os
file_size = os.path.getsize('E:\project-python\datafile.txt')
print('Size of file is', file_size, 'bytes')

After writing the above code (Python get the file size), Ones you will print “file_size” then the output will appear as a “ Size of file is 78 bytes ”. Here, we used getsize() function to get the size of the file. You can refer to the below screenshot Python get the file size.

Python get the file size
Get file size in Python

This is how we can get file size in Python.

Python get file extension from filename

To get the file extension from the filename string, we will import the os module, and then we can use the method os.path.splitext(). It will split the pathname into a pair root and extension.

Example:

import os
f_name, f_ext = os.path.splitext('file.txt')
print(f_ext)

After writing the above code (Python get file extension from the filename), Ones you will print “f_ext” then the output will appear as a “ .txt ”. Here, the filename will be split into two and when we print f_ext it will give the extension of the filename. You can refer to the below screenshot Python get a file extension from the filename.

Python get file extension from filename
Python get file extension from filename

This is how we can get file extension from filename in Python.

Python get filename without extension

To get the filename without extension in python, we will import the os module, and then we can use the method os.path.splitext() for getting the name.

Example:

import os
f_name, f_ext = os.path.splitext('file.txt')
print(f_name)

After writing the above code (Python get filename without extension), Ones you will print “f_name” then the output will appear as a “ file ”. Here, the filename will be split into two and when we print f_name it will remove the extension. You can refer to the below screenshot Python get filename without extension.

Python get filename without extension
Python get filename without extension

This is how to get the filename without extension in Python.

You may like the following Python tutorials:

In this Python tutorial, we discuss how to get filename from the path in Python and the below things:

  • How to get the filename from the path in Python
  • How to get file size in Python
  • How to get file extension from filename in Python
  • How to get filename without extension in Python

2 thoughts on “Python get filename from the path”

Comments are closed.