GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Extract the filename from a given path - w3resource

Python: Extract the filename from a given path

Last update on May 17 2025 05:43:20 (UTC/GMT +8 hours)


Extract Filename

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.


For more Practice: Solve these Related Problems:

  • Write a Python program to extract the file extension from a given path.
  • Write a Python program to extract the directory name from a given path.
  • Write a Python program to check if a given path points to a file or a directory.
  • Write a Python program to extract the base filename without the extension.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.