Python pathlib拼接目录
from pathlib import Path # 创建一个Path对象表示目录 base_directory = Path('C:/path/to/your/directory') # 使用/运算符来拼接子目录和文件名 file_path = base_directory / 'subdirectory' / 'myfile.txt' # 打印完整的文件路径 print(file_path) # 如果你需要字符串形式的路径,可以使用str()或者__str__()方法 print(str(file_path)) # 或者 print(file_path.__str__()) # 如果你需要Windows风格的路径(使用反斜杠\),可以调用as_posix()或as_uri()方法(取决于你的需求) # 但通常,Path对象会自动转换为正确的路径风格 print(file_path.as_posix()) # 在Windows上可能仍然显示为/,但在需要时会自动转换为\

浙公网安备 33010602011771号