Understand the absolute path and relative path in Python, and the absolute path in python

Source: Internet
Author: User

Understand the absolute path and relative path in Python, and the absolute path in python

This article describes the absolute path strength and relative path in Python, shares it with you, and gives yourself some notes.

1. absolute path

OS. path. abspath ("file name "):

Displays the absolute path strength of a file.

Eg:

>>> import os>>> os.chdir("E:\\PycharmProjects\\odycmdb\\odycmdb")>>> os.listdir()['settings.py', 'urls.py', 'wsgi.py', '__init__.py', '__pycache__']>>> os.path.abspath("settings.py")'E:\\PycharmProjects\\odycmdb\\odycmdb\\settings.py' 

2. Relative Path

OS. path. dirname ("file name "):

Displays the relative path of a file.

Eg:

>>> import os>>> os.chdir("E:\\PycharmProjects\\odycmdb\\odycmdb")>>> os.listdir()['settings.py', 'urls.py', 'wsgi.py', '__init__.py', '__pycache__']>>> os.path.dirname("settings.py")

3. Summary

Generally, absolute path functions and relative path functions are used together, especially before multiple file packages.

① OS. path. dirname (OS. path. abspath ("file name"): obtains the directory where the current folder is located.

Eg:

>>> import os>>> os.chdir("E:\\PycharmProjects\\odycmdb\\odycmdb")>>> os.listdir()['settings.py', 'urls.py', 'wsgi.py', '__init__.py', '__pycache__']>>> os.path.abspath("settings.py")'E:\\PycharmProjects\\odycmdb\\odycmdb\\settings.py'>>> os.path.dirname(os.path.abspath("settings.py"))'E:\\PycharmProjects\\odycmdb\\odycmdb' 

② OS. path. dirname (OS. path. dirname (OS. path. abspath ("file name"): Get the directory on the top of the current folder

Eg:

>>> import os>>> os.chdir("E:\\PycharmProjects\\odycmdb\\odycmdb")>>> os.listdir()['settings.py', 'urls.py', 'wsgi.py', '__init__.py', '__pycache__']>>> os.path.abspath("settings.py")'E:\\PycharmProjects\\odycmdb\\odycmdb\\settings.py'>>> os.path.dirname(os.path.dirname(os.path.abspath("settings.py")))'E:\\PycharmProjects\\odycmdb' 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.