How To Get Current Directory In Python
Last Updated : Aug 26, 2023
IN - Python
In this article we will show you the solution of how to get current directory in python, Python refers to the directory wherein a Python script is being run as the current working listing.
Obtaining the contemporary listing can be useful when you need to get right of entry to or adjust files and directories relative to the script's vicinity.
The os module gives the getcwd() feature, which retrieves the current operating directory.
Importing the os module & calling the getcwd() feature will go back a string figuring out the present operating directory.
Using this information, it is easy to study or write files within the present listing, navigate to other directories using relative paths, or perform different report moves.
Now move to the concept of how to get current directory in python.
Step By Step Guide On How To Get Current Directory In Python :-
import os def get_current_directory(): directory current_directory = os.getcwd() return current_directory current_directory = get_current_directory() print("Current Directory:", current_directory) new_directory = os.path.join(current_directory, "subdirectory") os.chdir(new_directory) updated_directory = get_current_directory() print("Updated Directory:", updated_directory)
- To access the operating system-related functions, we first import the required module os.
- The approach for acquiring the present day operating directory is therefore encapsulated in the get_current_directory() function.
- This manner makes use of the os.getcwd() method, which returns a string representation of the modern-day working directory.
- We can then call this technique again and again to get the modern directory.
- We retrieve the modern-day listing using the get_current_directory() technique and then upload the end result to the current_directory variable.
- This guarantees that the present day directory is available for future tasks.
- After printing the modern directory with print(), the direction of the listing is displayed in the terminal.
- After that, we will display you the way to trade the operating listing.
- To create a new route, use the os.path.join() characteristic, which joins the current_directory with the name of a subfolder.
- This creates a complete direction from the contemporary directory to the subfolder.
- Using the os.chdir() function, we alternate the operating directory to the newly created new_directory.
- Any destiny file actions might be relative to this new directory from this point ahead.
- We then execute the get_current_directory() function once more to acquire the updated current directory, which we then give to the updated_directory variable.
- To verify the change and see the new path in the console, we print this changed directory.
As a result, we were capable of apprehend how to collect the cutting-edge directory in Python.
Additionally, we observed that a number of file and directory movements in Python require getting access to the modern running listing.
The getcwd() characteristic of the os module can be used to speedy collect the contemporary directory.
With the aid of this understanding, we're capable of examine, write, and navigate throughout directories and files based on their relative paths.
I hope this article on how to get current directory in python helps you and the steps and method mentioned above are easy to follow and implement.