How to represent relative path in python

The following path introduces the expression of the following path when opening a file in a py file written for windows:

open('aaa.txt')open('/data/bbb.txt')open('D:\user\ccc.txt')

Among these three expressions, the first two are relative paths, and the third is absolute paths. The absolute path is easier to understand, it is the most complete path, the relative of the relative path is the incomplete path, this relative refers to the path relative to the current folder, in fact, it is the folder path where the py file you wrote! That is to say, the relative path you write must be the file a in the current folder A or the file in the folder B in A to be open.

Suppose the current location of the py folder is: D:\user\public

Then the paths of the files opened by the three lines of code are:

D:\user\public\aaa.txt
D:\user\public\data\bbb.txt
D:\user\private\ccc.txt

It is easy to understand that when you want to open the file where the py file is located, you only need to use the relative path, and to use other folders, you need to use the absolute path.

Note: We often use'/' to denote relative paths, and'' to denote absolute paths. The \ in the above path means escape. If you don't understand, you can Baidu by yourself.

In addition, web URLs and linux and unix systems generally use'/'

Of course, we can also get the absolute path of the current folder, as follows:

importos
path1=os.path.abspath('.')#Represents the absolute path of the current folder
path2=os.path.abspath('..')#Represents the absolute path of the upper level folder of the current folder

Therefore, we often set a path1 global variable to represent the current absolute path, plus a relative path to open the file that needs to be opened. This is done to avoid conflicts on different platforms, because different platforms represent the relative path There is a difference.

Knowledge point expansion:

1、 Absolute path

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

Shows the absolute path strength of a file

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"):

Shows the relative path of a file

import os

 os.chdir("E:\PycharmProjects\odycmdb\odycmdb")

 os.listdir()['settings.py','urls.py','wsgi.py','__init__.py','__pycache__']

 os.path.dirname("settings.py")

So far, this article on how to express the relative path in python is introduced. For more related python relative path writing content, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. I hope you will support ZaLou more in the future. Cn!

Recommended Posts

How to represent relative path in python
How to represent null values in python
How to wrap in python code
How to omit parentheses in Python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to save text files in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to learn python quickly
How to uninstall python plugin
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to understand python objects
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
How to use hanlp in ubuntu
How to use python thread pool
How to write python configuration file
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2
How to solve python dict garbled
How to install mysql in Ubuntu 14.04
How to view the python module
How to understand python object-oriented programming