J'ai un fichier à ressembler à ceci:
with open(file) as scope:
for line in scope:
line = line.strip()
if line.startswith('contains') and line.endswith('.h') or line.endswith('.hpp'):
scopeFileList.append(line.split()[-1])
3 Réponses :
Essayez ceci,
print(data) ['LRxAMEexception.csv', 'LAWODRxERROR.ddf', 'LAWODRxERRORtyp.h', 'LAWODRxERRORtyp.hpp']
Vous pouvez utiliser la fonction intégrée os.path.basename () code> pour obtenir uniquement le nom du fichier à partir d'un chemin:
from os.path import basename
with open(file) as scope:
for line in scope:
line = line.strip()
if line.startswith('contains') and line.endswith('.h') or line.endswith('.hpp'):
path = line.split()[-1]
scopeFileList.append(basename(path))
Essayez ceci: vous pouvez utiliser la recherche pour rechercher les noms de fichier à partir d'un chemin
'LRxAMEexception.csv' 'LAWODRxERROR.ddf' 'LAWODRxERRORtyp.h' 'LAWODRxERRORtyp.hpp'