SoFunction
Updated on 2024-10-29

Python Getting and Processing File Paths/Directory Paths Example Code

Description of the file directory structure

I. Path acquisition

1.1 Getting the absolute path of the current file

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

import os
file_path = (__file__)
print(file_path)

Output:

e:\Python\Path\python_path_test.py

1.2.1 Getting the directory where the current file is located

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

import os
directory_path = ((__file__))
print(directory_path)

Output:

e:\Python\Path

1.2.2 Getting the directory above the current file's directory

Use multiple **()** nested to get the directory one level up from the directory where the current file is located.

import os
parent_directory_path = (((__file__)))
print(parent_directory_path)

Output:

e:\Python

1.3 Getting the current file name

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

import os
file_name = (__file__)
print(file_name)

Output:

python_path_test.py

1.4 Getting the relative path of the current file to the base path

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

import os
file_path = (__file__) # Get the absolute path to the current file
base_path = "E:\Python" # Setting the base path (calculating the starting path of a relative path)
relative_path = (file_path, base_path) # Calculate the relative path from the 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

II. Path judgment

2.1 Determining whether a path exists

Use **()** to determine if a path exists.

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

Output:

True

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

Output:

False

2.2 Determining whether a path is absolute or not

Use **()** to determine if a path is an absolute path.

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

Output:

True

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

Output:

False

2.3 Determining whether a path is a directory

Use **()** to determine if a path is a directory.

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

Output:

True

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

Output:

False

2.4 Determining whether a path is a file

Use **()** to determine if the path is a file.

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

Output:

True

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

Output:

False

III. Path processing

3.1 Merge (concatenate) multiple directories/filenames into a single path

Use **(path1, path2, ----)** to merge (concatenate) multiple directory/filenames into a single path.

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

Output:

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

3.2 Split the path into a directory path (dirname) and a filename (basename).

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

import os
path = (__file__)
result = (path)
print(result)

Output:

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

3.3 Splitting paths into drive names (Windows OS) and file paths

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

import os
path = (__file__)
result = (path)
print(result)

Output:

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

3.4 Splitting paths into file paths and file extensions

Use **()** to split a path into a file path and a file extension.

import os
path = (__file__)
result = (path)
print(result)

Output:

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

P.S. Three solutions for python to read file paths correctly

I. Needs of the problem

When you use a program to read file data in daily life, it often displays some error messages such as file path does not exist.

II. Causes of the problem

These kinds of problems, in python, are mainly caused by "\" (backslashes).

This is caused by the fact that in Windows, a backslash (\) is used as a file path separator, but in python, a backslash (\) is recognized as an escape character. This causes the program to report errors.

III. Solutions

The following three solutions are commonly used to address the above problems.

Take the file path in Windows: "E:\CloudMusic\MV\" as an example.

Method 1 Replace the single backslash in the path with a double backslash. This is shown below:

“E:\CloudMusic\MV\”

Method 2 Add r in front of the path to keep the character in its original meaning. This is shown below:

r"E:\CloudMusic\MV\"

Method 3 Replace the backslash with a forward slash (/). This is shown below:

“E:/CloudMusic/MV/”

Just choose to adjust it according to your habits.

summarize

to this article on Python to get and handle file paths / directory paths to this article, more related to Python to get and handle file paths content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!