How to get full path of current file's directory in Python?



Python's OS module includes functions for creating and removing directories (folders), retrieving their contents, altering, and identifying the current directory, and more. To interface with the underlying operating system, you must first import the os module.

The location (path) of the executing program code can be obtained in Python. py with __file__. __file__ can be used to read other files based on the current file's location.

Example

In the following example, the os.getcwd() function produces a string str with the absolute path to the current working directory where Python is operating

#Python program to get the path of the current working directory #Program to get the path of the file #Using getcwd() #Importing the os module import os print(' The current working directory is: ', os.getcwd()) print('File name is: ', __file__)

Output

On executing the above program, the following output is generated.

The current working directory: C:\Users\pranathi\Desktop\python prog
File name: c:\users\pranathi\desktop\python prog\untitled1.py

Using os.path.basename()

In Python, the os.path.basename() method is used to get the base name of a path. To split the supplied path into a pair, this method uses the os.path.split() method internally (head, tail). After separating the supplied path into (head, tail) pairs, the os.path.basename() method returns the tail part.

Example

In the below example, the os.path.dirname() method is used to retrieve the directory name from the supplied path.

#python program to find the basename and dirname of the path import os print('basename of the file: ', os.path.basename(__file__)) print('dirname of the file: ', os.path.dirname(__file__))

Output

On executing the above program, the following output is generated.

basename of the file: untitled1.py
dirname of the file: c:\users\pranathi\desktop\python prog

Getting the absolute path of the directory

An absolute path refers to a file or folder's location regardless of the current working directory; in actuality, it is relative to the root directory.

Example

The following example is a python program to find the absolute path.

#python program to find the absolute path import os print('absolute path of the file: ', os.path.abspath(__file__)) print('absolute path of dirname: ', os.path.dirname(os.path.abspath(__file__)))

Using os.getcwd method in python

The OS module's getcwd() method returns a string with the absolute path to the current working directory. The trailing slash character is not included in the output string.

Example

In the following example, the os module is imported and the current working directory is obtained using the getcwd() function. The directory is printed using the print() function.

#importing the os module import os #to get the current working directory directory = os.getcwd() print(directory)

Output

On executing the above program, the following output is generated.

C:\Users\pranathi\Desktop\python prog

Example

The output will vary based on the directory you are in, but it will always begin with the root folder (for example, D:) and a directory prefixed by a.

import os absolute_path = os.path.abspath(__file__) print("Full path: " + absolute_path) print("Directory Path: " + os.path.dirname(absolute_path))

Output

On executing the above program, the following output is generated.

Full path: c:\users\pranathi\desktop\python prog\untitled1.py
Directory Path: c:\users\pranathi\desktop\python prog

Advertisements