All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Change Working Directory Python

Last Updated : Mar 11, 2024

Change Working Directory Python

In this article we will show you the solution of change working directory python, the best practice when dealing with files in directories in Python is to use absolute paths.

Understanding the current working directory and knowing how to find or change it is necessary when working with relative paths.

Relative paths specify the location of the file or directory starting from the current working directory, while absolute paths specify the location starting from the root directory.

A Python script is written in the working directory where it is running, which is the directory where it is executing.

Using the OS Python module is an easy way to perform operations on the operating system on a portable device.

In this module, we learn how to find the working directory of a file and how to change it. We will now discuss the idea of how to change the working directory in python with an example.

Step By Step Guide On Change Working Directory Python :-

Example 1

import os
# Function to Get the current
# working directory
def current_path():
    print("TC Current working directory before")
# Driver's code
# Printing Current Working Directory before
# Changing the Current Working Directory
# Printing Current Working Directory after

Example 2

# Import the os module
import os
path = 'C:\Users\Dell\Downloads\TC\'
   print("The Current working directory is: {0}".format(os.getcwd()))
except FileNotFoundError:
   print("Directory: {0} does not exist".format(path))
except NotADirectoryError:
   print("{0} is not a directory".format(path))
except PermissionError:
   print("No permissions to change to {0}".format(path))
  1. Creating a connection to the operating system is made possible by importing the os module.
  2. In the current_path function, define the current path.
  3. You should print a string in the current_path function to indicate that we are checking the current working directory.
  4. Using the os.getcwd() function, you can find out where the current working directory is located.
  5. A blank line will be printed for formatting purposes.
  6. An application's main function or driver code should be defined.
  7. You can print the current directory by calling the current_path function from main.
  8. Changing the current working directory to the parent directory is accomplished using the os.chdir() function.
  9. Print the new directory path after calling the current_path function again after changing the directory.

Conclusion :-

As a result, we have successfully learned how to change working directory in python with an example.

The os.getcwd() and os.chdir(path) functions in Python allow you to obtain the current working directory and modify it, respectively.

I hope this article on change working directory python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪