One of the most important aspects to consider when programming in almost any language is the directory of your project.

It’s always a good idea to use absolute paths when working in Python. The concept of changing or accessing the current working directory should only be familiar when working with relative paths.

By default, when you run a Python script, the current working directory is the directory where you run the script.

In this article, we will see how to query and change the current working directory in Python.

Also read : How are strings concatenated in Python?

Python’s os module has some very useful methods for dealing with directories. The method getcwd() of the os module is used to get the path to your current working directory.

Note that you must import the `module os’ to use this method.  The method produces the path as a string without a trailing slash.

import os
cwd = os.getcwd()
print(Current working directory: {0}.format(cwd))

The above snippet prints the path to the current working directory where your script is running.

Also read : How are strings concatenated in Python?

If for some reason you need to change your current working directory, we need another method in the os module, the chdir() method.

The method takes the destination path as an argument, which can be relative or absolute.

import os
print(Current working directory: {0}.format(os.getcwd())))
print(Current working directory: {0}.format(os.getcwd())

The above snippet first prints your current working directory, then goes to the /tmp directory and prints the path again.

Here are some exceptions that can occur if this method is used incorrectly.

  • NotADirectoryError : The argument passed by chdir() can only be a directory. If not, you get this exception.
  • FireNotFoundError: If the destination folder does not exist, a FireNotFoundError exception is thrown.
  • ResolutionError: This exception is thrown if the script does not have the necessary permissions to access the destination folder.

Also read : How do I start Python in the terminal?

The os module of Python has many useful methods that can be used for various operations. Here are some commonly used ones.

  • os.listdir() : Displays all files and subfolders in the folder.
  • os.mkdir(‘dirName’) : Creates a new folder with the attached name.
  • os.rename(‘oldName’, ‘newName’) : Renames any file or folder in the current hot folder with the specified name and changes it to the new name.
  • os.remove(*filename’) : Removes the specified file from the hot folder.
  • os.rmdir(‘dirName’) : Removes the specified empty folder from the hot folder.
  • shutil.retree(‘dirName’) : Removes the specified non-empty folder from the hot folder. Note that you must import the shutil library before using this method.

Also read : How to separate strings in Python?

The one who writes/cuts/films/owns all the technology, and when he’s not around, switches to virtual machine races. You can contact Yadullah at [email protected], or follow him on Instagram or Twitter.

frequently asked questions

How do I find a working directory?


How do I get the current working directory in the terminal?

‘ Code ‘ linux-current-direct…

Related Tags:

change current working directory pythonpython get current directory pathhow to change directory in pythonpython get current file directorypython get directory of current scriptpython list files in current directory,People also search for,Feedback,change current working directory python,python get current directory path,how to change directory in python,python get current file directory,python get directory of current script,python list files in current directory,python set working directory command line,python get directory from path

Related Post

Leave a Reply

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