Kodeclik Blog
About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
Think of your computer like a big filing cabinet with lots of folders inside folders. When you're writing a Python program that needs to work with files (like saving a game score or reading a list of your favorite movies), you need to know which drawer of the filing cabinet you're working in. This is what we call the "present working directory" or PWD. Another name for it is CWD, or "current working directory".
Just like you need to know which folder your homework is saved in so you can find it later, your Python program needs to know where its files are. For example, if you're making a game that saves high scores, you want to make sure it saves the score file in the right place so it can find it again when the player comes back to play later.
So let us talk about how to find the present working directory (PWD) in Python. There are at least three ways to do so!
The first method uses the os.getcwd() function, which is the most straightforward and commonly used approach that returns a string representing the current working directory.
The output will be something like:
Of course, your result will differ depending on the location where your Python program is run.
The second method employs the more modern pathlib module, introduced in Python 3.4, which returns a Path object instead of a string, offering more flexibility for path manipulation.
The output will be the same as before.
The third method uses os.path.abspath() combined with file, which is particularly useful when you need to get the directory of the currently executing script rather than the working directory.
The output is again:
The main difference between these methods lies in their return types - os.getcwd() returns a string, Path.cwd() returns a Path object, and both can be used effectively depending on your specific needs. The third approach, i.e., os.path.abspath(os.path.dirname(file)) returns a string that represents the absolute path to the directory containing the current Python script file. These methods are cross-platform compatible and will work correctly on Windows, macOS, and Linux systems.
Now that we have seen how to print the current working directory, lets talk about when (and why) you will need to do this. Understanding the “when” is crucial for several common development scenarios.
Going back to our example at the beginning of this blogpost, lets write a Python program to save your scores:
As shown, the program above finds the current working directory, prints a useful message about it (so you know what it is!) and then saves the highscore in a file. Now if you go looking for this file on your computer, you know where to find it, because the program has helpfully printed the directory for you. Without it, you might forget where you ran the program from and might have to search for it first.
Similarly, when you're working on bigger projects, like making a game with images and sound files, you'll need to tell Python where to find these files. It's like telling a friend how to find your house - you need to give them the right directions from where they are. The PWD helps your program find its way around your computer.
Sometimes you might run your program from different places on your computer. Maybe you're working on it at school and then at home. Using the PWD helps your program work correctly no matter where you run it from. It's like having a GPS for your program - it always knows where it is and can find its way to the files it needs.
In summary, file operations represent one of the most frequent use cases for finding the PWD (present working directory). When your application needs to load configuration files, read data files, or access resources that are stored relative to your script's location, knowing the PWD becomes essential. This is particularly important when writing output files such as logs, reports, or generated data, as you need to ensure they're saved in the correct location that your application can consistently access.
If you liked this blogpost, checkout our other "os" related blogposts, such as os.chdir(),os.listdir(), and os.path.join().
Want to learn Python with us? Sign up for 1:1 or small group classes.
import os
current_dir = os.getcwd()
print("Current directory:", current_dir)
Current directory: /home/kodeclik/example
from pathlib import Path
current_dir = Path.cwd()
print("Current directory:", current_dir)
import os
current_dir = os.path.abspath(os.path.dirname(__file__))
print("Current directory:", current_dir)
Current directory: /home/kodeclik/example
import os
# Find out where we are
current_folder = os.getcwd()
print(f"Your program is running from: {current_folder}")
# Save a high score
score = 1000
with open("highscore.txt", "w") as file:
file.write(str(score))