Categories
Python Answers

How to run the shell ‘cd’ command to change the working directory with Python?

Spread the love

Sometimes, we want to run the shell ‘cd’ command to change the working directory with Python.

In this article, we’ll look at how to run the shell ‘cd’ command to change the working directory with Python.

How to run the shell ‘cd’ command to change the working directory with Python?

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.

For instance, we write:

import os

path = './'
os.chdir(path)

to change the current working directory to path.

Conclusion

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *