Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
2 views
in Python by (10.2k points)
I am facing problems in _files which isn't defined if I execute the script with exec, ececfile and in _module_ which is only defined for modules.

And now I want to know which is the best way to check current script directory in Python.

 Can someone help me with this?

2 Answers

0 votes
by (46k points)
edited by

Use pathlib module to perform your task, for ref. check the syntax below:

from inspect import currentframe, getframeinfo
from pathlib import Path

filename = getframeinfo(currentframe()).filename
parent = Path(filename).resolve().parent

I am using this from Python 3.4 and it also works in all versions above that, but I am not sure about the older ones.

Happy Learning

0 votes
by (106k points)

You can use the below-mentioned code:-

os.path.dirname(os.path.abspath(__file__))

You can use the following video tutorials to clear all your doubts:-

Related questions

+1 vote
2 answers
0 votes
1 answer
asked Jun 26, 2019 in Python by Aarav (11.4k points)
+3 votes
2 answers
+2 votes
3 answers
asked May 24, 2019 in Python by Suresh (3.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...