• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Get and Change the Current Working Directory in Python

Written by Admin, Updated On October 17, 2020
python
python-get-change-current-working-directory

In Python, it’s a best practice to use the absolute paths. It’s little bit difficult while you are working with the relative paths. An absolute path specifies a file or directory location starting from the root directory, while the relative path begins from the current working directory. At the time of execution of Python script, you current working directory is set the directory from where you run the script.

Usign os python module, it can interact with the operating system. Python includes this module in the standard python library and way for finding and changing the current working directory.

Getting the Current Working Directory in Python#

Use the getcwd() method of the os module in Python, which will return a string containing the absolute path of the current working directory. The returned string does not include the trailing slash character.

os.getcwd()

You must import the module at the top of the file to use the os module method.

Following is the example to display the current working directory:

# Import the os module
import os

# Get the current working directory
cwd = os.getcwd()

# Print the current working directory
print("Current working directory: {0}".format(cwd))

# Print the type of the returned object
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))

It will show the output as following:

Current working directory: /home/linuxize/Desktop
os.getcwd() returns an object of type: <class 'str'>

Change the Current Working Directory in Python#

Use the chdir() method to change the current working directory in Python.

os.getcwd(path)

You should pass the path of the directory where you want to change as an argument of this method. The path argument can be absolute or relative.

Below is an example:

# Import the os module
import os

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

# Change the current working directory
os.chdir('/tmp')

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

The output will look something like this:

Current working directory: /home/tecnstuff/Desktop
Current working directory: /tmp

Make sure that the argument passed in to the chdir() method must be a directory, otherwise it will throw and NotADirectoryError exception. If the directory doesn’t exist, a FileNotFoundError exception is raised. It will show the PermissionError exception if the user doesn’t have required permissions.

# Import the os module
import os

path = '/var/www'

try:
    os.chdir(path)
    print("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError:
    print("Directory: {0} does not exist".format(path))
except NotADirectoryError:
    print("{0} is not a directory".format(path))
except PermissionError:
    print("You do not have permissions to change to {0}".format(path))

Conclusion#

You have learned how to find the current working directory in Python and how to change the current working directory in Linux.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Start, Stop, or Restart Nginx
Next Article   Touch Command in Linux

Related Posts

  • How to Install Python on Ubuntu 22.04

    How to Install Python on Ubuntu 22.04

    February 18, 2023
  • How to Install Python 3.11 on Debian 11

    How to Install Python on Debian 11

    January 25, 2023
  • How to Install Python 3.9 on CentOS 8

    How to Install Python 3.9 on CentOS 8

    December 31, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install PHP 8.2 on Debian 11
    How to Install PHP 8.2 on Debian 11 Linux February 24, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook