All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Change Working Directory

Last Updated : Mar 11, 2024

Python Change Working Directory

In this article we will show you the solution of python change working directory, a working directory is a current directory in which we are working and from which the script is run; within this directory, we have access to many files (the ones located within it).

However, we sometimes need to change directories and go back and forth between files and folders.

Python's os module is required in order to change the working directory.

There is nothing to install because it is already there. For interaction, management, and modification of files and directories on the system, Python normally uses the OS module.

Now move to the concept of python change working directory.

Step By Step Guide On Python Change Working Directory :-

A portable method of interacting with operating system is made available through the OS Python Module.

The module, which is a component of the standard Python library, offers resources for finding and adjusting the working directory.

Change the working directory by using Python's chdir() function.

The path to the directory that want to visit is the only parameter that the method accepts. You can choose between an absolute path and a relative route.

import os
current_directory = os.getcwd()
print("Your current working directory is %s" % current_directory)
new_working_directory = "C:\\Users\\Dell\\OneDrive\\Desktop\\PythonDirectory"
try:
    os.chdir(new_working_directory)
    print("The working directory has been changed!")
    print("WD: %s " % os.getcwd())
except NotADirectoryError:
    print("You have not chosen a directory.")
except FileNotFoundError:
    print("The folder was not found. The path is incorrect.")
except PermissionError:
print("There is no access for this folder/file.")
  1. The os module is imported to begin our work. Finally, to obtain the current working directory, we employ the getcwd() method.
  2. So that we can print the current working directory, we use the print method.
  3. We then changed the working directory's path.
  4. The chdir technique is then used with path in a try-except clause that we define after that.
  5. With the chdir() method in Python, a current working directory is changed to the specified path.
  6. After that, print statement is used. The working directory will be transformed into an alternative working directory using the chdir() technique if the path already exists.
  7. In the event that the path is not a directory, an error will be generated.
  8. Then, we use NotADirectoryError to raise an error stating that your route must go to a directory of some kind.
  9. Finally, in cases where the path is invalid, we utilise FileNotFounderror. There will be a FileNotFoundError raised. The directory we are pointing to either doesn't exist or wasn't found, according to this error.
  10. Then, when the user lacks sufficient permissions to access or edit the selected directory, PermissionError is raised.

Conclusion :-

Hence, we were able to understand the idea behind Python's changing working directory.

We also discovered that changing the working directory in Python requires the os module.

It is already there, therefore nothing is required to install. Python often makes use of the OS module for file and directory interface, management, and change.

The chdir(path) technique is the only one needed to change working directories, making it simple.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪