Skip to content

Python Program to Get the File Name From the Full Path

Category
Python
Versions & support
About Python →
On this page

Python, as an incredibly versatile language, offers several ways to manipulate strings and paths. A frequent necessity in programming is to extract the file name from a full file path. Whether you are handling user uploads or manipulating files on the server, this skill is incredibly useful. In this article, we’ll explore a Python program that can extract a file name from a complete file path.

Using Python’s os.path module

Python’s built-in `os` module provides a plethora of functions to handle file and directory operations, and the `os.path` module is designed specifically for path manipulations. For this task, we’ll use the `os.path.basename()` function.

Here is a simple program that takes a file path and gives you the file name:

When you run this program, it will print myfile.txt, which is the name of the file from the provided path.

The function `os.path.basename(path)` in Python gives the base name in the path, which is the second element from the tuple produced when the path is passed to the `os.path.split(path)` function. It’s important to highlight that this function behaves differently compared to the Unix `basename` program. For instance, while the Unix `basename` program returns ‘bar’ for ‘/foo/bar/’, the `os.path.basename()` function in Python instead returns an empty string (”).

Using Python’s pathlib module

Another alternative to handle paths in Python is the `pathlib` module. It offers a more high-level and intuitive approach to handle filesystem paths. Here is a similar program using `pathlib`:

When you run this program, it will print myfile.txt, same as the previous program. `Path.name` provides the final path component, without its suffix.

In the `pathlib` module, Path is a class that encapsulates filesystem paths and offers methods and properties to handle paths according to their semantics. When you create a Path object, you can access properties such as name, suffix, and stem that represent different components of the path.

Error Handling

In real-life applications, you should add error handling to these functions. A potential issue can arise if the provided input isn’t a string, or if it doesn’t represent a valid path. Here’s an example of how you might handle these errors:

In this code, `isinstance(file_path, str)` checks if `file_path` is a string, and `path.is_absolute()` checks if `path` represents an absolute path. If either check fails, the function raises a ValueError with a description of the problem.

This is just the beginning of what you can do with file paths in Python. The os.path and pathlib modules offer many more functions to manipulate paths, check if a path exists, create new directories, and much more.

With Python, handling file and directory paths is an easy task, and understanding how to use these modules effectively will help you become a better Python programmer.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top