Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 987197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:26:29+00:00 2026-05-16T05:26:29+00:00

How do I get the current file’s directory path? I tried: >>> os.path.abspath(__file__) ‘C:\\python27\\test.py’

  • 0

How do I get the current file’s directory path?
I tried:

>>> os.path.abspath(__file__)
'C:\\python27\\test.py'

But I want:

'C:\\python27\\'
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:26:30+00:00Added an answer on May 16, 2026 at 5:26 am

    The special variable __file__ contains the path to the current file. From that we can get the directory using either pathlib or the os.path module.

    Python 3

    For the directory of the script being run:

    import pathlib
    pathlib.Path(__file__).parent.resolve()
    

    For the current working directory:

    import pathlib
    pathlib.Path().resolve()
    

    Python 2 and 3

    For the directory of the script being run:

    import os
    os.path.dirname(os.path.abspath(__file__))
    

    If you mean the current working directory:

    import os
    os.path.abspath(os.getcwd())
    

    Note that before and after file is two underscores, not just one.

    Also note that if you are running interactively or have loaded code from something other than a file (eg: a database or online resource), __file__ may not be set since there is no notion of "current file". The above answer assumes the most common scenario of running a python script that is in a file.

    References

    1. pathlib in the python documentation.
    2. os.path – Python 2.7, os.path – Python 3
    3. os.getcwd – Python 2.7, os.getcwd – Python 3
    4. what does the __file__ variable mean/do?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the current executable's file path without the executable name at
I want to get list of methods in current file, when my tool is
I'm trying to get the current executable's file path without the executable name at
I wrote a function to get the full path of the current file under
Possible Duplicate: get name of current PHP script in include file Can an included
My current application needs to get data from a file to initialize its attributes.
I am trying to get current running context in android, I tried to use:
I want to get current alpha of textview i am using the following code
Is there any way to get current function name in C++? I want to
I want to search the current location in my app, but I see entire

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.