Categories
Python Answers

How to extract file name from path, no matter what the os/path format with Python?

Spread the love

Sometimes, we want to extract file name from path, no matter what the os/path format with Python.

In this article, we’ll look at how to extract file name from path, no matter what the os/path format with Python.

How to extract file name from path, no matter what the os/path format with Python?

To extract file name from path, no matter what the os/path format with Python, we can use the os.path.basename method.

For instance, we write

import os

print(os.path.basename(your_path))

to get the file name from the your_path path string with os.path.basename.

Conclusion

To extract file name from path, no matter what the os/path format with Python, we can use the os.path.basename method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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