Home > Article > Backend Development > python3 gets the upper-level directory instance of the current file
This article mainly introduces the example of python3 getting the upper-level directory of the current file. It has certain reference value. Now I share it with you. Friends in need can refer to it
Because using python selenium has Sometimes I need to get the upper-level directory of the current file. I found it after searching for a while and recorded it here;
os.path.dirname(os.path.dirname(os.path .abspath(__file__))) This is to get the upper-level directory of the current file
##sys.path.append(os.path.dirname(os.path .dirname(os.path.abspath(__file__)))) This is to add the path to the system environment variable
os.path.abspath(os. path.join(os.path.dirname(__file__),"..")) #Get the root path of the current project
My own example:
A and B folders are at the same level. There is a.py under folder A. Now I want to reference a.py of folder A in folder B. At this time I can add sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) to B’s file. In this case, I can directly import a and reference aThe above is the detailed content of python3 gets the upper-level directory instance of the current file. For more information, please follow other related articles on the PHP Chinese website!