Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

Python os.chdir()

The Python os.chdir() method within the os module takes a file path as argument and changes the current working directory to the given path.

Python os.chdir()

Let us explore this method by creating a repl and opening a Python file.

Now let us create a Python program in it:

import os

directory = os.getcwd()

print(directory)

To support your understanding this is how our setup looks like:

Python os.chdir()

In the program above, we first import the “os” module in Python. Then we obtain the current working directory (cwd) using the getcwd() command and save it in the variable called “directory”. Note that the output of this command will be different in different environments. When we print the directory, we get the following output (your output might vary):

/home/runner/MedicalOrneryGame

Let us create a directory called "newfolder" within our repl:

Python os.chdir()

We can now update our program to change to this directory and then do the getcwd() command again and print the new current working directory:

import os

directory = os.getcwd()
print(directory)

os.chdir("newfolder")

directory = os.getcwd()
print(directory)

The output is:

/home/runner/MedicalOrneryGame
/home/runner/MedicalOrneryGame/newfolder

As you can see the getcwd() command has updated the current working directory. From this point, any new Python operations (e.g., creating and writing to a file) we do in our program will happen in this new directory.

In the above program we used the chdir() method with a relative path name, i.e., we just said “newfolder” and it is assumed that it will be added to the current working directory. You could have also given an absolute path name like so:

import os

directory = os.getcwd()
print(directory)

os.chdir(directory + "/" + "newfolder")

directory = os.getcwd()
print(directory)

The above program will give the same output as before. Note that in the chdir() function we have specified the current directory (through the variable “directory”), then added the “newfolder” to the path with a slash (“/”) in between. If you omit the slash, you will get a non-existent directory and the program will complain. For instance, if you do:

import os

directory = os.getcwd()
print(directory)

os.chdir(directory + "newfolder")

directory = os.getcwd()
print(directory)

You will get:

/home/runner/MedicalOrneryGame
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    os.chdir(directory + "newfolder")
FileNotFoundError: [Errno 2] No such file or directory: 
    '/home/runner/MedicalOrneryGamenewfolder

In summary, os.chdir() is a valuable Python function because it enables you to change your working directory from within your program instead of having to go to your Terminal or prompt and do it manually. If you understand how it works and you have files distributed across your directory system, this is a very useful function to seamless navigate directories within your programs!

In this blogpost, we have seen how Python’s os.chdir() function can help change directories from within your Python program. If you liked this, checkout our blogpost on the Python os.mkdir() function which as you can surmise helps create directories! Also checkout os.listdir() which lists the contents of a directory and os.remove() which removes files from within Python!

For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

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.

Copyright @ Kodeclik 2024. All rights reserved.