Find the current directory and executed file's directory in Python (2024)

The ProblemJump To Solution

In a Python script, how can I discover both the directory I executed the script from and the directory in which the script resides?

The Solution

We can find both of these directories using functions in Python’s built-in os module. For example:

Click to Copy

import os# Find the directory we executed the script from:execution_dir = os.getcwd()# Find the directory in which the current script resides:file_dir = os.path.dirname(os.path.realpath(__file__))# Print resultsprint(f"Execution dir: {execution_dir}")print(f"File dir: {file_dir}")

If we place this script in /tmp and run it from our home directory, it will produce the following output:

Click to Copy

Execution dir: /home/userFile dir: /tmp

os.getcwd returns the current working directory.

os.path.dirname returns the name of a supplied path, and os.path.realpath() returns the real path of a provided filename, resolving any shortcuts or symbolic links. __file__ is a built-in constant containing the pathname of the file from which the running Python module was loaded. In some instances, such as when code is executed in an interactive interpreter rather than from a file, this constant may not be set and accessing it will return an exception.

Find the current directory and executed file's directory in Python (2024)
Top Articles
U.S. wealth distribution over time by generation 2023 | Statista
Animal Spirits: Meaning, Definition in Finance, and Examples
13 Easy Ways to Get Level 99 in Every Skill on RuneScape (F2P)
Toyota Campers For Sale Craigslist
Www.politicser.com Pepperboy News
Chelsea player who left on a free is now worth more than Palmer & Caicedo
The Best Classes in WoW War Within - Best Class in 11.0.2 | Dving Guides
Acbl Homeport
Tiger Island Hunting Club
Blue Beetle Showtimes Near Regal Swamp Fox
2024 Non-Homestead Millage - Clarkston Community Schools
Rainfall Map Oklahoma
Gon Deer Forum
Jenn Pellegrino Photos
Mzinchaleft
Canvas Nthurston
iZurvive DayZ & ARMA Map
All Obituaries | Buie's Funeral Home | Raeford NC funeral home and cremation
What Is Vioc On Credit Card Statement
Craigslist Clinton Ar
Sodium azide 1% in aqueous solution
15 Primewire Alternatives for Viewing Free Streams (2024)
Finding Safety Data Sheets
Roanoke Skipthegames Com
Shelby Star Jail Log
Yale College Confidential 2027
Safeway Aciu
Cfv Mychart
Unreasonable Zen Riddle Crossword
Angel Haynes Dropbox
Movies - EPIC Theatres
Mobile crane from the Netherlands, used mobile crane for sale from the Netherlands
How Do Netspend Cards Work?
JD Power's top airlines in 2024, ranked - The Points Guy
Petsmart Distribution Center Jobs
Exploring TrippleThePotatoes: A Popular Game - Unblocked Hub
Hotels Near New Life Plastic Surgery
Ktbs Payroll Login
Invalleerkracht [Gratis] voorbeelden van sollicitatiebrieven & expert tips
Puretalkusa.com/Amac
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Obituaries in Hagerstown, MD | The Herald-Mail
062203010
Engr 2300 Osu
Craigslist Rooms For Rent In San Fernando Valley
844 386 9815
Best Haircut Shop Near Me
Florida Lottery Powerball Double Play
Sea Guini Dress Code
Jane Powell, MGM musical star of 'Seven Brides for Seven Brothers,' 'Royal Wedding,' dead at 92
Bedbathandbeyond Flemington Nj
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5687

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.