Python implements methods to get the current directory and the parent directory

Source: Internet
Author: User
This article mainly introduces Python to obtain the current directory and the implementation of the parent directory, the need for friends can refer to the following

Gets the path to the current file:


From OS import path  d = path.dirname (__file__) #返回当前文件所在的目录  # __file__ is the current file, if running this line in the IDE will give an error, instead #d = Path.dirname ('.')

To get the parent directory for a path:


Parent_path = Os.path.dirname (d) #获得d所在的目录, which is the parent directory of D parent_path = Os.path.dirname (parent_path) # #获得parent_ The directory where path is located is the parent directory of Parent_path

Get the absolute path to the specification:


Abspath = Path.abspath (d) #返回d所在目录规范的绝对路径

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.