Menu Close

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

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.

Posted in Python, Python Answers