Home  >  Article  >  Backend Development  >  python3 gets the upper-level directory instance of the current file

python3 gets the upper-level directory instance of the current file

不言
不言Original
2018-05-02 15:47:242330browse

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 a


The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn