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 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.
The above code, we can use to get filename from a path in Python.
You may also like, How to use Pandas drop() function 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.
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.
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.
This is how to get the filename without extension in Python.
You may like the following Python tutorials:
- Create a hello world program in python using Visual Studio Code
- Python String Functions
- Python TypeError: ‘list’ object is not callable
- How to convert an integer to string in python
- How to concatenate strings in python
- Python naming conventions
- How to split a string using regex in python
- How to create a string in Python
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
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.
Hi Bijay,
that solution to get file name appears incorrect as per: https://docs.python.org/3/library/os.path.html#os.path.basename
and the below from IDLE:
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
>>> import os
>>> print(os.path.basename(‘E:\project-python\string\list.py’))
E:\project-python\string\list.py
>>>
I hope it helps.
ignore last comment, I tested the windows path in a linux interpreter… duh!