+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 (46.1k 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 (107k 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
+3 votes
2 answers
0 votes
1 answer
asked Jun 26, 2019 in Python by Aarav (11.5k points)
+2 votes
2 answers
asked May 23, 2019 in Python by Anvi (10.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...