Python Change Working Directory
Last Updated : Apr 9, 2023
IN - Python
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.")
- The os module is imported to begin our work. Finally, to obtain the current working directory, we employ the getcwd() method.
- So that we can print the current working directory, we use the print method.
- We then changed the working directory's path.
- The chdir technique is then used with path in a try-except clause that we define after that.
- With the chdir() method in Python, a current working directory is changed to the specified path.
- 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.
- In the event that the path is not a directory, an error will be generated.
- Then, we use NotADirectoryError to raise an error stating that your route must go to a directory of some kind.
- 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.
- Then, when the user lacks sufficient permissions to access or edit the selected directory, PermissionError is raised.
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.