Sometimes we need to know the full path of the file which we are working on to decrease the error. So by this tutorial we will learn to get the full path or directory of a particular file. So without wasting time lets learn about How do I get the full path of a file in Python.
Contents
How do I get the full path of a file in Python
To get the full path of a file in python you can use these methods which will give you the full path of the file in which you are working on.
- use pathlib
- Use absolute()
- Use os module
These all the methods are very easy and useful. So here we are going to learn these all methods. So without wasting your time lets learn it.
- get the full path of a file in Python
To get the full path of a file in Python use pathlib. By using pathlib you can get the full path of the file which you are working on. Lets see this by given example:
import pathlib print(pathlib.Path(__file__).parent.absolute())
Output :d:\Python Tutorial
- How do I get the full path of a file in Python
To get the full path of a file in Python just Use absolute(). By using absolute() you can get the full path of the file which you are working on. Lets see this by given example:
import pathlib print(pathlib.Path().absolute())
Output :d:\Python Tutorial
- How do I get the full path of a file in Python
To get the full path of a file in Python just Use os module.By using os module you can get the full path of the file which you are working on. Lets see this by given example:
import os print(os.path.dirname(os.path.abspath(__file__)))
Output :d:\Python Tutorial
Method 1: Use pathlib
By using pathlib you can get the full path of the file which you are working on. First of all import pathhib and the use pathhib.path() to get the full path of the file. Lets see this by given example:
import pathlib
print(pathlib.Path(__file__).parent.absolute())
Output :
d:\Python Tutorial
Method 2: Use absolute()
Use absolute() to get the full path of the file which you are working on. First of all inport pathlib and then use absolute() function which will help you to get the full path of a file. Lets see this by given example:
import pathlib
print(pathlib.Path().absolute())
Output :
d:\Python Tutorial
Method 3: Use os module
First of all import os and then use os.path.dirname and you can get the full path of the fil in which you are working on. By using os module you can get the full path of the file which you are working on. Lets see this by given example:
import os
print(os.path.dirname(os.path.abspath(__file__)))
Output :
d:\Python Tutorial
Conlusion
Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.