Skip to main content
 首页 » 编程设计

python之简单的字符串遇到

2025年05月04日20xing901022

我如何取一个字符串并且只取字符串中的某个点? 如果我有字符串:

testSetting = f"C:\Users\bgb\Documents\Brent\Code\Variable\Visual Studio\Local.testsettings" 

我需要的字符串的唯一部分是 Local.testsettings 部分,但我不知道如何完成这个,我知道你可以使用 string.strip() 但每次触发文件路径时,文件路径中表示 variable 的部分都会发生变化。 (文件路径存储在一个列表中,并且在循环中)

请您参考如下方法:

There's a module for that .

>>> import os.path 
>>> testSetting = r"C:\Users\bgb\Documents\Brent\Code\Variable\Visual Studio\Local.testsettings" 
>>> os.path.basename(testSetting) 
'Local.testsettings' 

还有补充功能:

>>> os.path.dirname(testSetting) 
'C:\\Users\\bgb\\Documents\\Brent\\Code\\Variable\\Visual Studio'