Get Path of File Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python, the file plays an important role when data needs to be stored permanently. We need to know the correct path of the file to read or write to it, or to perform various operations on it; otherwise, it will throw an error. Suppose you are working in a directory and you want to make some changes to the file that is in the same directory, then you have to know the relative path. If we want to make some changes to a file in another directory, then we have to know the absolute path of that directory.

Get Path of File Using Python

Introduction:

In Python, we have various built-in functions which can help us to get the path of the running .py file(the file on which we are currently working). These functions are path. cwd(), os.getcwd(), pathlib.Path().absolute(), os.path.basename, and os.path.abspath. We will talk about all the functions in detail later in the next section. 

Let me discuss a bit about relative and absolute paths as we have used these words above. An absolute path specifies the location of the file relative to the root directory or it contains the complete location of the file or directory, whereas relative paths are related to the current working directory.

Absolute path: C:/users/Dell/docs/Scaler.txt

If our current working directory(CWD) is C:/users/Dell/, then the relative path to Scaler.txt would be docs/Scaler.txt

CWD + relative path = absolute path

Note:

  • Using __file__, we can also get the absolute path of the running file.
  • __file__ is callable while working in a file. If we try to call it from the shell interpreter, it will not work.
  • __file__ does not work in the Jupyter notebook.

Prerequisites:

Before going deep into this topic, you should have at least some knowledge of basic Python file operations. These operations include open, close, read, write, and delete.

Different Ways to Get Path of File in Python

Finally, the wait is over. We will talk about all the functions that we can use to get the path of the file Python. Let's talk about them one by one.

Using Path.cwd():

Path.cwd() is used to get the current path. The pathlib module provides us with the function cwd() by which we can fetch the current working directory.

We don't have to pass parameters to the cwd() function.

Code:

Output:

As you can see in the above output, we are not getting the room in which we are sitting, which means we are not getting the actual path of the running Python file. So to get the whole path, we can make use of __file__.

Code:

Output:

In the above output, we are getting the absolute path of our running Python file.

Using os.getcwd():

The function os.getcwd() returns the current working directory. Here getcwd stands for Get Current Working Directory.

Code:

Output:

Using pathlib.Path().absolute():

pathlib.Path().absolute() is used to get the current path. The pathlib module provides us with the function absolute() by which we can fetch the path of the current working directory.

Code:

Output:

Using os.path.basename():

The OS module provides numerous functions. os.path.basename() returns the name of the currently running Python file. And if we want to get the whole path of the directory in which our Python file is residing, then we can use os.path.dirname().

Code:

Output:

In the above output, you can see that we have got the name of our Python file(test.py) and also the path of the directory where we have saved our Python file(Lang directory).

Now, instead of getting the whole path for the directory, can we only get the directory name in the output? The answer is yes.

Code:

Output:

In the above output, as you can see, instead of the whole path, we have got the directory name.

Using os.path.abspath():

os.path.abspath() function is very much the same as the os.path.basename() function. In the os.path.basename() function, we were getting the name of the python file, but using the os.path.abspath() function we will get the absolute path of the Python file.

Code:

Output:

In the above output, we can see that os.path.abspath() has provided us with the absolute path of our Python file. Similarly, using os.path.dirname(), we will get the name of the directory in which our Python file is residing.

Conclusion:

Let's summarise our topic, get path of file Python by discussing some of the important points.

  • In Python, we have various built-in functions which can help us to get the path of the running .py file(the file on which we are currently working).
  • These functions are path.cwd(), os.getcwd(), pathlib.Path().absolute(), os.path.basename, and os.path.abspath.
  • The function os.getcwd() returns the current working directory. Here getcwd stands for Get Current Working Directory.
  • os.path.basename() returns the name of the currently running Python file.
  • If we want to get the whole path of the directory in which our Python file is residing, then we can use os.path.dirname().