![](/img/trans.png)
[英]Saving file with a name containing / at the destination folder in python
[英]Extract file name with the folder containing the file
如果我有以下文件路徑:
'G:\\My Drive\\A\\B\\C\\EH\\2021_12_17.txt'
如何使用包含文件的文件夾獲取文件名:
'EH\\2021_12_17.txt'
您可以使用Split()
進行組合(我確定肯定有更多 Pythonic 方式):
s='G:\\My Drive\\A\\B\\C\\EH\\2021_12_17.txt'
file_name = s.split("\\")[-2] + "\\\\" + s.split("\\")[-1]
嘗試join
str.split
path = 'G:\\My Drive\\A\\B\\C\\EH\\2021_12_17.txt'
'\\'.join(path.split('\\')[-2:]). # -> 'EH\\2021_12_17.txt'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.