0
votes

Lire une chaîne spécifique d'une ligne dans un fichier

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])


0 commentaires

3 Réponses :


0
votes

Essayez ceci,

print(data)

['LRxAMEexception.csv',
 'LAWODRxERROR.ddf',
 'LAWODRxERRORtyp.h',
 'LAWODRxERRORtyp.hpp']


0 commentaires

1
votes

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))


0 commentaires

0
votes

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'


0 commentaires