Max*_*603 6 python-3.x pathlib
假设我有一个未知长度的列表。如何使用 pathlib 将此列表的所有元素加入到我当前的路径中?
from pathlib import Path
Path.joinpath(Path(os.getcwd()).parents[1] , *["preprocessing", "raw data"])
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为该函数需要字符串而不是元组。
Mis*_*agi 10
构造函数pathlib.Path直接接受多个参数:
>>> Path("current_path" , *["preprocessing", "raw data"])
PosixPath('current_path/preprocessing/raw data')
Run Code Online (Sandbox Code Playgroud)
Path.joinpath仅当您已有预先存在的基本路径时才使用:
>>> base = Path("current_path")
>>> base.joinpath(*["preprocessing", "raw data"])
PosixPath('current_path/preprocessing/raw data')
Run Code Online (Sandbox Code Playgroud)
例如,要获取相对于“工作目录但向后退一步”的路径:
>>> base = Path.cwd().parent
>>> base.joinpath(*["preprocessing", "raw data"])
PosixPath('/Users/preprocessing/raw data')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2641 次 |
| 最近记录: |