w3resource

Python: Extract the filename from a given path

Python Basic: Exercise-103 with Solution

Write a Python program to extract the filename from a given path.

Sample Solution:

Python Code:

# Import the 'os' module for operating system-related functions.
import os

# Print a newline for clarity.
print()

# Use 'os.path.basename' to extract the filename component from the given path.
# In this case, it extracts the filename 'homework-1.py' from the provided path.
print(os.path.basename('/users/system1/student1/homework-1.py'))

# Print another newline for clarity.
print()

Sample Output:

homework-1.py

Flowchart:

Flowchart: Extract the filename from a given path.

Python Code Editor:

 

Previous: Write a Python program to get system command output.
Next: Write a Python program to get the effective group id, effective user id, real group id, a list of supplemental group ids associated with the current process.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-basic-exercise-103.php