You can use the Python scripting language to interact with your operating system and navigate between files. The process is similar to using a command line interface.
get current directory, or Python
os.getcwd(), allows you to change directories and move files around. This article covers what
get current directory is and how to use the command.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
What is Python ‘get current directory’?
get current directory, or
os.getcwd(), method consists of two components:
os, which stands for
operating system and
.getcwd(), which stands for
get current working directory — referring to whichever directory (folder) or location your code file is currently stored in.
You need to import the os module before using the method. A Python module is pre-built code; you don’t need to install any third-party libraries. The os module allows us to navigate through the operating system’s file system, get file information, and move files around.
How Python ‘get current directory’ works
Let’s see the Python get current directory method in action.
If you open a new code file, say in IDLE (Python’s built-in code editor), you can type this code:
import os print(os.getcwd())
With “import os”, we are importing the os module into our program. With “print”, we are telling the program to display the file’s current location. When you run this code, the output will be the name of the folder that you are currently in, which is the location of the current directory.
If your code file is saved to your desktop, the output may look like this:
Note that “name” refers to whatever name is associated with your computer’s user account (usually it is your first name).
Using ‘get current directory’ with ‘change directory’
Once you know what directory you are working in, you may want to navigate to a different directory. To do this, you can use the ‘change directory’ method,
As an example, let’s suppose that you want to check what directory you are in, then navigate to the Documents directory on your computer (assuming you are not already in the Documents directory), and then confirm that you are in the Documents directory.
Here is how you would view your current directory, change directories, and then confirm you are in the Documents directory. Check out this code:
import os print(os.getcwd()) os.chdir('/Users/name/Documents') print(os.getcwd())
Note that in the above code we first have to import the os module. Then, we print the current working directory using the “print” command. Next, we change the directory using the
os.chdir() method, with the name of the desired directory (Documents) in the target folder path. Finally, we print the current working directory again to confirm that we are in the Documents directory.
Here is the output of this code:
This means that the first directory was the Desktop, and the new directory is Documents. Again, we used the “print” command twice–once to display the current directory before we changed it, and once to display the current directory after we changed it.
Because Python is a scripting language, you can use Python to communicate with your operating system to navigate through, and give you information about, the files on your computer. The Python ‘get current directory’ method is one Python method that you can use to get information about your files.
Some developers find that interacting with files with a scripting language such as Python is more productive than using an operating system’s GUI (graphical user interface) to create and click into folders and files. Knowing how to use Python or another scripting language to interact with your operating system is a valuable skill to have as a developer.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.