Python obtains and processes file path/directory path

Description of file directory structure

insert image description here

1. Path acquisition

1.1 Get the absolute path of the current file

Use **os.path.abspath()** to get the absolute path of the current file.

import os
file_path = os.path.abspath(__file__)
print(file_path)

output:

e:\Python\Path\python_path_test.py

1.2.1 Get the directory where the current file is located

Use **os.path.dirname()** to get the directory where the current file is located.

import os
directory_path = os.path.dirname(os.path.abspath(__file__))
print(directory_path)

output:

e:\Python\Path

1.2.2 Get the upper level directory of the directory where the current file is located

Use multiple nested **os.path.dirname()** to get the upper level directory of the directory where the current file is located.

import os
parent_directory_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(parent_directory_path)

output:

e:\Python

1.3 Get the current file name

Use **os.path.basename()** to get the directory where the current file is located.

import os
file_name = os.path.basename(__file__)
print(file_name)

output:

python_path_test.py

1.4 Get the relative path of the current file to the reference path

Use **os.path.relpath(file_path, base_path)** to get the relative path of the current file to the base path.

import os
file_path = os.path.abspath(__file__) #获取当前文件的绝对路径
base_path = "E:\Python" #设置基准路径(计算相对路径的起始路径)
relative_path = os.path.relpath(file_path, base_path) #计算从基准路径开始的相对路径
print("Absolute Path: " + file_path)
print("Base Path: " + base_path)
print("Relative Path: " + relative_path)

output:

Absolute Path: e:\Python\Path\python_path_test.py
Base Path: E:\Python
Relative Path: Path\python_path_test.py

2. Path judgment

2.1 Determine whether the path exists

Use **os.path.exists()** to determine whether the path exists.

import os
path = "./data/data_file.txt"
is_exists = os.path.exists(path)
print(is_exists)

output:

True
import os
path = "./data/data.txt"
is_exists = os.path.exists(path)
print(is_exists)

output:

False

2.2 Determine whether the path is an absolute path

Use **os.path.isabs()** to determine whether the path is an absolute path.

import os
path = "E:\Python\Path\data\data_file.txt"
is_exists = os.path.isabs(path)
print(is_exists)

output:

True
import os
path = "./data/data_file.txt"
is_exists = os.path.isabs(path)
print(is_exists)

output:

False

2.3 Determine whether the path is a directory

Use **os.path.isdir()** to determine whether the path is a directory.

import os
path = "E:\Python\Path\data"
is_exists = os.path.isdir(path)
print(is_exists)

output:

True
import os
path = "E:\Python\Path\data\data_file.txt"
is_exists = os.path.isdir(path)
print(is_exists)

output:

False

2.4 Determine whether the path is a file

Use **os.path.isfile()** to determine whether the path is a file.

import os
path = "E:\Python\Path\data\data_file.txt"
is_exists = os.path.isfile(path)
print(is_exists)

output:

True
import os
path = "E:\Python\Path\data"
is_exists = os.path.isfile(path)
print(is_exists)

output:

False

3. Path processing

3.1 Merge (connect) multiple directories/file names into one path

Use **os.path.join(path1, path2, ···)** to merge (join) multiple directories/file names into one path.

import os
path1 = "E:\Python"
path2 = "Path\data"
path3 = "data_file.txt"
path = os.path.join(path1, path2, path3)
print(path)

output:

E:\Python\Path\data\data_file.txt

3.2 Split the path into the path of the directory where the file is located (dirname) and the file name (basename)

Use **os.path.split()** to split the path into the path of the directory where the file is located (dirname) and the file name (basename).

import os
path = os.path.abspath(__file__)
result = os.path.split(path)
print(result)

output:

('e:\\Python\\Path', 'python_path_test.py')

3.3 Split path into drive name (Windows OS) and file path

Use **os.path.splitdrive()** to split a path into a drive name (Windows) and a file path.

import os
path = os.path.abspath(__file__)
result = os.path.splitdrive(path)
print(result)

output:

('e:', '\\Python\\Path\\python_path_test.py')

3.4 Split path into file path and file extension

Use **os.path.splitext()** to split the path into file path and file extension.

import os
path = os.path.abspath(__file__)
result = os.path.splitext(path)
print(result)

output:

('e:\\Python\\Path\\python_path_test', '.py')

Guess you like

Origin blog.csdn.net/qq_45899597/article/details/128416394
Recommended