Fran Coronas Martin
Fran Coronas Martin

Reputation: 17

How I get the string from the combination of the last part of some paths?

First of all, I have this list of tuples:

paths = [('/User/folder/fileA.png', '/User/folder/fileB.png'), ('/User/folder/fileC.png', '/User/folder/fileD.png'), ('/User/folder/fileE.png', '/User/folder/fileF.png') ]

And my objective would be to get a function that could give me the string of the combined files:

print(combinations)

fileA_fileB
fileC_fileD
fileE_fileF

I have tried to fix it with the pathlib module but I can't iterate through the list. Any ideas? Thank you very much in advance

Upvotes: 1

Views: 97

Answers (1)

python_user
python_user

Reputation: 7083

You can convert them to Path objects then get the name using Path.stem. You can of course concatenate them how you wish, adding an underscore in your case. Something like f'{Path(i).stem}_{Path(j).stem}'.

from pathlib import Path

paths = [('/User/folder/fileA.png', '/User/folder/fileB.png'), ('/User/folder/fileC.png', '/User/folder/fileD.png'), ('/User/folder/fileE.png', '/User/folder/fileF.png') ]
res = [Path(i).stem + Path(j).stem for i, j in paths]

for i in res:
    print(i)

Output

fileAfileB
fileCfileD
fileEfileF

Upvotes: 2

Related Questions