How to Get Current Directory in Python

0
30
How to Get and Print Current Directory in Python

While working on a Python program, you either work on an absolute or a relative path. If you use an absolute path, then it is advantageous because you explicitly tell the compiler what path to work with. Still, sometimes, you work with a relative path, and that is where you have to deal with the current working directory.

Before diving deep into the current working directory, let’s understand what a directory is and what is the current working directory. Then we will see how to get and print the current working directory.

What is a directory?

A directory is a collection of subdirectories and files. To access and manipulate files and directories in Python, use the os module that provides methods and functions to operate on it.

What is the current working directory?

The current working directory(CWD) is where you are currently working. Every process has a current directory. When a process starts, it merely inherits the current directory from its parent process.

Current working directory on Linux and Mac

To get a current working directory on Linux and Mac operating systems, use the pwd (print working directory) command.

Current working directory on Windows

To get the current working directory on Windows GUI, go to Windows Explorer. Inside windows explorer, you can see the current working directory in a file address bar at the top of the explorer window.

To get the current working directory on the Windows command line or CLI, see the current working directory displayed as the prompt. For example, if the prompt was “C:\Windows\Program Files>” the “Program Files” directory is the current directory.

How to get current working directory in Python

To get the current working directory in Python, use the os.getcwd() function. The os.getcwd() is a built-in method that returns the location of the current working directory.

How to print a current working directory in Python

To print the current working directory in Python, use the print(os.getcwd()) function. The print() function prints the output of the os.getcwd() function, which is the path to the current working directory.

Syntax

os.getcwd()

Parameters

The os.getcwd() function does not get any argument.

Return value

The os.getcwd() function returns a string representing the current working directory.

How to use the os.getcwd() function?

To use the os.getcwd() function in Python, import the os module. The os module provides functions and utilities to engage in operating system-related tasks.

import os

cwd = os.getcwd()
print(cwd)

Output

/Users/krunallathiya/Desktop/Code/R

My current working directory is the R folder. So that’s why it prints up to the R folder’s path.

How to get the full directory path of a Python file

To get a full path to the directory of a Python file, use the os.path.dirname(), os.path.realpath(), and __file__ constant. The os.path.dirname() is a built-in method to get the directory name from the provided path.

The os.path.realpath() is a built-in function that returns the canonical path of the provided filename by removing any symbolic links discovered in the path.

The __file__ is a constant variable in Python that contains the path to the imported module.

import os

curr_file = os.path.dirname(os.path.realpath(__file__))
print(curr_file)

Output

/Users/krunallathiya/Desktop/Code/R

My current file is app.py which is inside the R folder, and that’s why it returns a folder path up to the R folder.

Conclusion

The best way to get the current working directory in Python, use the os.getcwd() function. The os.getcwd() function will return the current directory path on Linux, Mac, and Windows and use the print() function to print it.

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.