开发目的
在实际工作中,笔者经常遇到特殊的文件夹路径或者文件路径,由于缺乏经验需要在此浪费较多时间。进而想要写一个路径标准化函数,用 pathlib 包替换 os 包,把输入路径转成标准的可读格式,即输入可能是带有双引号的路径或者路径间隔是“\”、“\”、“/” 等转成标准格式。
原代码
def normalize_path(path):
path = re.sub(r'^["\']|["\']$', '', path.strip())
path = path.replace('/', '\\')
path = path.replace('\\\\', '\\')
return path
优化过程
1.0
- 使用
pathlib内置方法,这些方法通常是用C实现的,效率高 - 优先考虑了绝对路径和相对路径、内存和避免Path的报错
from pathlib import Path
import os
def normalize_path(path_input: str) -> Path:
"""
Convert input path string to standardized Path object.
Handles quotes, different separators, and resolves relative paths.
"""
# Remove quotes and extra whitespace
clean_path = path_input.strip().strip('"').strip("'")
# Convert to Path object and resolve to absolute path
try:
path = Path(clean_path).resolve()
return path
except (OSError, ValueError):
# Return unresolved path if resolution fails
return Path(clean_path)
2.0
- 使用
pathlib.Path自动处理不同操作系统的路径分隔符 - 优化去除字符串两端的引号和空格
- 当路径可解析时,使用
resolve()方法解析相对路径和符号链接,返回最准确的解析结果 - 当路径不可解析时,提供合理的回退方案,回退到使用
absolute()方法
import os
import logging
from pathlib import Path
from typing import Union
def normalize_path(input_path: Union[str, Path]) -> Path:
"""
标准化路径函数
将带有各种引号、不同分隔符的路径转换为标准的Path对象
处理以下情况:
- 去除双引号、单引号
- 统一处理"\\"、"/"和"\"分隔符
- 解析相对路径和特殊符号(如".", "..")
参数:
input_path: 输入的路径字符串或Path对象
返回:
标准化的Path对象
"""
# 如果已经是Path对象,直接返回
if isinstance(input_path, Path):
return input_path.resolve()
if not isinstance(input_path, (str, Path)):
raise TypeError(f"输入路径必须是字符串或Path对象,收到 {type(input_path)}")
# 移除字符串两端的引号和空格
path_str = str(input_path).strip(' \t\n"\'')
# 使用Path创建路径对象,pathlib会自动处理不同平台的分隔符
path_obj = Path(path_str)
# 解析路径(解决相对路径、符号链接等)
try:
return path_obj.resolve()
except (OSError, PermissionError, FileNotFoundError) as e:
# 记录错误日志
logging.warning(f"文件系统错误: {e}, 使用绝对路径作为回退方案")
return path_obj.absolute() # 返回绝对路径作为回退方案
except Exception as e:
logging.warning(f"路径解析失败: {e}, 使用绝对路径作为回退方案")
return path_obj.absolute() # 返回绝对路径作为回退方案
import os
import logging
from pathlib import Path
from typing import Union
def normalize_path(input_path: Union[str, Path]) -> Path:
"""
Path normalization function.
Converts paths with various quotes and different separators to a standardized Path object.
Handles the following cases:
- Removal of double quotes, single quotes
- Uniform handling of "\\", "/", and "\" separators
- Resolution of relative paths and special symbols (e.g., ".", "..")
Parameters:
input_path: Input path string or Path object.
Returns:
Normalized Path object.
"""
# Type validation
if not isinstance(input_path, (str, Path)):
raise TypeError(f"Input path must be a string or Path object, received {type(input_path)}")
# Return directly if already a Path object
if isinstance(input_path, Path):
return input_path.resolve()
# Remove quotes and whitespace from both ends of the string
path_str = str(input_path).strip(' \t\n"\'')
# Create a Path object - pathlib automatically handles platform-specific separators
path_obj = Path(path_str)
# Resolve path (handles relative paths, symbolic links, etc.)
try:
return path_obj.resolve()
except (OSError, PermissionError, FileNotFoundError) as e:
# Log error
logging.warning(f"Filesystem error: {e}, proceeding with absolute path as fallback")
return path_obj.absolute() # Return absolute path as fallback solution
except Exception as e:
# Log error
logging.warning(f"Unknown error encountered during path resolution: {e}, returning absolute path")
return path_obj.absolute() # Return absolute path as fallback solution
2949

被折叠的 条评论
为什么被折叠?



