![](/img/trans.png)
[英]How to get the full path of a file in python including the subdirectory?
[英]Get beginning of path including specified subdirectory
我正在尋找一種從給定路徑創建新路徑的方法,其中新路徑包括原始路徑的開頭並以原始路徑的指定子目錄結束。
原始路徑: /foo/bar/foobar.txt
指定的子目錄: 'bar/'
新路徑: /foo/bar/
import pathlib
orig = pathlib.Path('/foo/bar/foobar.txt')
print(orig.parents[0])
# Prints:
# /foo/bar
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.