Python get filename from the path

By: On:

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

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

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

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

You may like the following Python tutorials:

In this tutorial, we learned how to get filename from path in Python, python get the file size, python get file extension from filename and also we have seen python get filename without extension.

2 thoughts on “Python get filename from the path”

Leave a Comment