• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python dirname – Get Directory Name of Path using os.path.dirname()

Python dirname – Get Directory Name of Path using os.path.dirname()

February 27, 2022 Leave a Comment

The Python dirname() function is very useful for getting the directory name of a given path.

import os

print(os.path.dirname("/Home/Documents/example.py")

#Output:
/Home/Documents

When working with file systems, it can be useful to be able to get the directory name and path of a file.

The Python os module provides us with a number of great functions to be able to perform many operating system tasks.

With the os module, we can easily get the directory name of a path.

To get the directory name of a path, we can use the os.path.dirname() function.

Pass a string representing the path of a file, and you will get the full path of the directory of that file.

Below is an example of how to use the Python dirname() function to get the directory name of a file.

import os

print(os.path.dirname("/Home/Documents/example.py")

#Output:
/Home/Documents

Hopefully this article has been useful for you to learn how to get the directory name of a path using the Python dirname() function.

Other Articles You'll Also Like:

  • 1.  How to Subtract Two Numbers in Python
  • 2.  How to Group By Columns and Find Variance in pandas
  • 3.  Using Python to Calculate Average of List of Numbers
  • 4.  pandas to_pickle – Write DataFrame to Pickle File
  • 5.  Create Empty String Variable in Python
  • 6.  Apply Function to All Elements in List in Python
  • 7.  pandas max – Find Maximum Value of Series or DataFrame
  • 8.  Using Python to Check If List of Words in String
  • 9.  Python Increment Counter with += Increment Operator
  • 10.  Write Integer to File Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x