file_systems = ["c:","d:","f:"] # Your possible file systems here
file_extensions = [".txt",".csv", ".xml"] # Your file extensions here
# my text
text = r"hello. this is path: C:\Users\zivsi\noz\wave.txt"
# The position where the path starts/ends
idx_fs = 0
idx_fe = 0
for i in range(len(text)):
test_fs = text[i:i+2].lower()
test_fe = text[i:i+4].lower()
# Find the position where your file system starts
if test_fs in file_systems:
idx_fs = i
if test_fe in file_extensions:
idx_fe = i + 3
break
path = text[idx_fs:idx_fe]
print(path) # This gives as result: C:\Users\zivsi\noz\wave.txt
尝试使用正则表达式来匹配字符串中的特定模式,请参阅re-library文档(正则表达式库)的here
我认为您可能会尝试在您的路径中找到一些文件系统。假设要查找的所有路径都包含文件系统。我将尝试这样做:
我知道这有一个有限的用例,但它与您提供的路径一起工作,请让我知道这是否是您正在寻找的!:D
相关问题 更多 >
编程相关推荐