In this python tutorial, you will learn about the Os change directory python also we will check:
- How to change directory using chdir()
- Check the current working directory
- Changing the directory exceptions
- Python os change directory name
- Python os get absolute path of the current directory
How to change directory using chdir()
Now, we can see how to change directory using chdir() in python.
- In this example, I have imported the module as os.
- The os.chdir is used to change the directory.
- When the directory is changed the Changed Directory is printed as the output.
Example:
import os
os.chdir(r"C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work")
print("Changed Directory")
The below screenshot shows the output.
This is how to change directory using chdir() in Python.
You may like, How to Print Python Fibonacci series.
Check current working directory in Python
Here, we can see how to check the current working directory in python.
- In this example, I have imported a module called os.
- Here, we are using os.chdir and the path.
- The os.getcwd() returns the current directory.
- To get the output, I have used print(“Current directory :”, cwd).
Example:
import os
cwd = os.getcwd()
print("Current directory :", cwd)
We can see the current directory as the output. You can refer to the below screenshot.
This code we can use to check current working directory in Python.
Changing directory exceptions in Python
Now, we can see how to changing the directory exceptions in python.
- In this example, I have imported a module called sys and os.
- The os.getcwd() returns the current directory
- The wrong directory is assigned as fd = ‘work/office’.
- The try block is used to check the block of the code.
- The except is used if any error occurs.
- The finally block is executed and the os.getcwd is used to get the current directory.
Example:
import sys, os
cwd = os.getcwd()
fd = 'work/office'
try:
os.chdir(fd)
print("Inserting inside-", os.getcwd())
except:
print("Wrong directory")
finally:
print("Restoring the path")
os.chdir(cwd)
print("Current working directory is-", os.getcwd())
You can refer to the below screenshot for the output.
Python os change directory name
Now, we can see make os change directory name in python.
- In this example, I have imported a module called os. The source path and destination path are assigned and in the destination path, the name of the new directory is written as an office.
- The os.rename(source, destination) is used to rename the directory.
- I have used print(“Renamed successful.”) to get the output.
Example:
import os
source = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder'
destination = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\office'
os.rename(source, destination)
print("Renamed successful.")
The below screenshot shows the output.
This is how we can change the directory name in Python OS.
Python os get absolute path of the current directory
Now, we can see os get absolute path of the current directory in python.
- In this example, I have imported a module called module os.
- The os.chdir is used to change the current directory to specify the current directory.
- The os.path.abspath is used to find the absolute path.
Example:
import os
os.chdir(r"C:\Users\Administrator.SHAREPOINTSKY\Desktop\office")
print(os.path.abspath("work"))
We can see absolute path as the output. You can refer to the below screenshot for the output.
The above code we can use to get absolute path of current directory in Python.
You may like the following Python tutorials:
- Python program to reverse a string with examples
- Python program to find sum of n numbers with examples
- Python program to print prime numbers
- File does not exist Python
- How to read video frames in Python
- Python Multiprocessing
In this Python tutorial, we have learned about the Os change directory python. Also, we covered these below topics:
- How to change directory using chdir().
- Check the current working directory
- Changing the directory exceptions
- Python os change directory name
- Python os get absolute path of the current directory
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.