簡體   English   中英

如何獲取 python 中包含子目錄的文件的完整路徑?

[英]How to get the full path of a file in python including the subdirectory?

我嘗試了所有方法,但它根本不起作用。 我有一個位於子目錄中的文件,並且該子目錄位於目錄中。 調用類似的東西

os.path.abspath(__file__))

只產生

directory\\file

但是我需要

directory\\subdirectory\\file

應該有一個簡單的方法來做到這一點,對吧? 我不知道為什么 abspath 無法識別子目錄。

你應該看看pathlib 模塊

from pathlib import Path

my_path = Path("my_file.txt")
full_path = Path.resolve()

然后,如果需要,您可以將 full_path 轉換為字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM