Cas de combat réel Python - séparez le chemin du fichier, le nom du fichier et l'extension du chemin complet

problème

Séparez le chemin du fichier, le nom du fichier et l'extension du chemin en fonction du chemin d'
accès complet = 'C: \ ProgramData \ Dell \ InventoryCollector \ Log \ ICDebugLog.txt'

réponse

>>> path = 'C:\\ProgramData\\Dell\\InventoryCollector\\Log\\ICDebugLog.txt'
>>> print(path)
C:\ProgramData\Dell\InventoryCollector\Log\ICDebugLog.txt

# 定义 path 路径
path = 'C:\\ProgramData\\Dell\\InventoryCollector\\Log\\ICDebugLog.txt'
# 获取文件路径
>>> ls1 = path.split('\\')
>>> ls1
['C:', 'ProgramData', 'Dell', 'InventoryCollector', 'Log', 'ICDebugLog.txt']
>>> ls2 = ls1[0:len(ls1)-1:]
>>> ls2
['C:', 'ProgramData', 'Dell', 'InventoryCollector', 'Log']
>>> new_path = '\\'.join(ls2)
>>> print(new_path)
C:\ProgramData\Dell\InventoryCollector\Log

#获取文件名
>>> ls1[len(ls1)-1]
'ICDebugLog.txt'

#获取文件名后缀
>>> tt = ls1[len(ls1) -1]
>>> tt
'ICDebugLog.txt'
>>> tt.split('.')
['ICDebugLog', 'txt']
>>> ls = tt.split('.')
>>> ls[1]
'txt'

Je suppose que tu aimes

Origine blog.csdn.net/XY0918ZWQ/article/details/111198023
conseillé
Classement