I have selected several files with the open multiple files dialog:

[[u'C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\chord.jpg'], [u'C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\toad1.jpeg', u'C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\21369.jpg', u'C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\g2.jpg']]

I want to display the selected files in a nice user-friendly way. Just the filenames, each on a separate line. No paths, commas or brackets.

Can someone nudge me in the right direction please? Thanks.

Dani AI

Generated

was on the right track: extract the final path component and present one name per line. The selection dialog in this thread returns a nested sequence of path strings, so first flatten that structure, then take the tail of each path, optionally remove duplicates while preserving order, and finally join with newlines for display.

from itertools import chain
import os

# assume paths_nested is the nested list returned by the file dialog
flat = [p for p in chain.from_iterable(paths_nested)]
names = [os.path.split(p)[1] for p in flat]        # final path component
names = list(dict.fromkeys(names))                 # remove duplicates, keep order
display_text = "\n".join(names)

Notes: use os.path helpers rather than manual string splitting so the code works across platforms (see the os.path.split docs). For very old Python versions or code that receives Unicode literals, ensure the display sink (console, GUI widget, file) can accept the encoding; encode to UTF-8 for legacy consoles or use Unicode-capable UI widgets. See the itertools.chain reference for flattening patterns and the Python Unicode HOWTO for encoding guidance.

os.path.split documentation
itertools.chain documentation
Python Unicode HOWTO

Recommended Answers

All 2 Replies

Use os.path.basename(path) to get the filename from the path.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.