Is there a way to find out the name and path of the running file?

Asked 2 years ago, Updated 2 years ago, 47 views

Creating code to recall another script file. Before calling another script, I want to find out the name and path of the script I'm running right now, how do I do it?

For example, Invoke script_1.py -> script_2.py Invoke script_2.py -> script_3.py script_3.py -> Call Nothing

script_3 must find that script_3 is itself script_3 without passing the factor in script_2

os.getcwd() could not be written by returning the path of the first script. What else can I do?

python file scripting

2022-09-21 19:17

1 Answers

The method of finding out your own name and your directory can be written as follows:

execfile("p2.py")
import inspect, os
print inspect.getfile(inspect.currentframe()) #path at p2.py
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # Directory to which p2.py belongs


2022-09-21 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.