In Python 3.4+, you can use the pathlib module (included in Python's standard library):

>>> from pathlib import Path
>>> p = Path("/home/user/Downloads/repo/test.txt")
>>> print(p.stem)
test
>>> print(p.name)
test.txt
Answer from smheidrich on Stack Overflow
🌐
Medium
medium.com › @johnusa4567 › python-get-filename-from-path-simple-methods-explained-a5ea7623b38c
Python Get Filename from Path: Simple Methods Explained | by John Usa | Medium
October 4, 2025 - Both Windows and Linux paths are supported by these functions, but using pathlib can help reduce inconsistencies across platforms. Working with file paths is a common requirement in many projects. By using python get filename from path methods such as os.path.basename or pathlib.Path.name, you can manage files more efficiently while keeping your code clean and readable.
Discussions

How to get just the file name from a file path.
You may get some benefit from os.path , and in particular os.path.basename() . More on reddit.com
🌐 r/learnpython
9
3
November 11, 2021
python - How to get folder name, in which given file resides, from pathlib.path? - Stack Overflow
Is there something similar to os.path.dirname(path), but in pathlib? More on stackoverflow.com
🌐 stackoverflow.com
‘With’ statement vs ‘pathlib’ method for opening files
There is no pathlib method for opening files. pathlib is, as the name suggests, a library for dealing with paths. It is not in any way an alternative to opening files inside a context manager (with), which is responsible for doing things like cleaning up file handles when you are finished with them. In fact the two work together well, for example: directory = pathlib.Path('dir') file_path = directory / "filename" with file_path.open() as f: ... Maybe you should post an example of what you think has changed? More on reddit.com
🌐 r/learnpython
23
4
April 18, 2024
What is the obvious way to open a file: open( ) or pathlib/Path?
I say, default to pathlib.Path and its methods, and use open if you Need to pass a file object to something Need to save memory and cannot fit the entire file to it at once Need to append data There may be other cases I didn't immediately think of. Reading and writing all the file contents at once is in my opinion preferable, because by minimising the amount of time the file is kept open, you can alleviate the risk of race conditions - even if you don't use multiprocessing, if the user deletes the file while you're in the middle of reading it, your program might not be designed to handle that. pathlib, in my opinion, should nowadays be treated as the canonical way to handle files. I'd even go as far to say that unless there's a need for supporting older Python versions, pathlib.Path.open is preferable to open (they're basically the same thing, but the path argument is provided by the pathlib.Path instance automatically). The documentation may not always follow the latest trends and recommendations, and there are reasons for that. Hence the discrepancy. More on reddit.com
🌐 r/learnpython
8
9
March 18, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-get-the-file-name-from-the-file-path
Python Program to Get the File Name From the File Path - GeeksforGeeks
July 23, 2025 - The standard utility modules for Python include this module. Although stem is one of the utility attributes that enables extracts of the filename from the link without extension if we want an extension with the file we can use name attributes ... from pathlib import Path file_path = 'C:/Users/test.txt' # stem attribute extracts the file # name print(Path(file_path).stem) # name attribute returns full name # of the file print(Path(file_path).name)
🌐
FavTutor
favtutor.com › blogs › get-filename-from-path-python
03 Methods to Get Filename from Path in Python (with code)
December 16, 2022 - The code implementation using the ... different operating systems. Inside the pathlib library, we can use the Path function to get the filename from a given path....
🌐
Vultr
docs.vultr.com › python › examples › get the file name from the file path
Python Program to Get the File Name From the File Path
April 10, 2025 - Since directory_path here ends ... module, which provides object-oriented file path handling. Use Path.name from the pathlib module to get the file name....
🌐
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
dirpath is a Path to the directory ... To get a full path (which begins with self) to a file or directory in dirpath, do dirpath / name....
🌐
datagy
datagy.io › home › file handling › python: get filename from path (windows, mac & linux)
Python: Get Filename From Path (Windows, Mac & Linux) • datagy
February 10, 2023 - You’ll learn how to get the filename form a Python path using the os library, the string .split() method, and the popular pathlib library.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how to get just the file name from a file path.
r/learnpython on Reddit: How to get just the file name from a file path.
November 11, 2021 -

I am just wondering how I would be able to save just the file name from a file path. Right now I have the program opening the file explorer and saving the file path to a variable. I am just wondering how I could trim this to save just the name of the file to another variable. Current code:

filepath = askopenfilename(
  filetypes=[("SH File", "*.sh"), ("All Files", "*.*")]
    )
if not filepath:
    return
🌐
Python Guides
pythonguides.com › python-get-filename-from-the-path
Get Filename From Path in Python (pathlib and os.path)
September 16, 2025 - To get the filename from a path in Python, use Path(path).name from pathlib or os.path.basename(path).
🌐
eSparkBiz
esparkinfo.com › path from any os/path format
How to Extract File Name from Path in Python Easily?
July 30, 2025 - ... Scalable, Secure, and High-Performance Solutions for Your Business. ... In Python, you can extract just the filename from a full path using either os.path.basename() or pathlib.Path().name.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-filename-from-path-without-extension-using-python
Get filename from path without extension using Python - GeeksforGeeks
October 3, 2025 - Python · import os path = ... .split('.', 1)[0] takes the part before the first dot. With pathlib, you wrap your path in Path(path) and read its .stem attribute to get the filename without extension....
🌐
TradingCode
tradingcode.net › python › filename-without-last-extension
Get filename with extension removed: here's how in Python
To get the filename without the last extension from those directory items, here’s what we do: from pathlib import Path # Specify the directory to get the filenames without extension from dir_path = Path(r"C:\Wallpapers") # Loop over all directory ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Filename, Directory, Extension from a Path String in Python | note.nkmk.me
May 8, 2023 - UNIX (including Mac) uses the slash ... · source: os_path_basename_dirname_split_splitext.py · Use os.path.basename() to get the filename (basename) from a path string....
🌐
Real Python
realpython.com › python-pathlib
Python's pathlib Module: Taming the File System – Real Python
5 days ago - You can create a unique filename by constructing a path with a counter in a loop, checking for the existence of the file using .exists(), and incrementing the counter until you find a filename that doesn’t exist.
🌐
Programiz
programiz.com › python-programming › examples › file-name-from-file-path
Python Program to Get the File Name From the File Path | Programiz
March 14, 2024 - Python Strings · import os # file ...e_name)[0]) Output · file · basename() gives the name of the last file/folder of the path, whereas splitext() splits the file name into filename and extension. import os print(os.path.spli...
🌐
CodeRivers
coderivers.org › blog › get-filename-from-path-python
Retrieving Filenames from Paths in Python - CodeRivers
February 22, 2026 - import os # Absolute path path ... and provides an object-oriented approach to working with file paths. If you create a Path object from a file path, you can access the name attribute to get the filename....
🌐
Python Pool
pythonpool.com › home › tutorials › get a filename from a path in python: pathlib and os.path
Get a Filename from a Path in Python: pathlib and os.path
July 13, 2026 - To get a filename from a path in Python, use Path(path).name from pathlib. To get the filename without the extension, use Path(path).stem.
🌐
YouTube
youtube.com › modernpython
How to get filename without extension from path in Python - YouTube
Use pathlib's Path.stem to get filename without extension.
Published: September 28, 2022
Views: 325
🌐
TestDriven.io
testdriven.io › tips › 01895f3a-6f68-4790-b4cd-7483d106e63b
Filename pattern matching in Python with pathlib.Path.glob()
You can use pathlib.Path.glob() to list all files matching a given pattern. For example, you can list names of all txt files located inside the same directory as your Python script👇