G
G
GLEK THE GREAT2021-08-27 23:31:55
Python
GLEK THE GREAT, 2021-08-27 23:31:55

How to truncate the path so that only the filename remains?

In general, I made a png to jpg image converter (and others), in the tkinter window I choose which method and the file selection window opens in the explorer and this is written to the filename variable and I need to somehow trim the path leaving only the file name (because the path is different each time) so that ""im = Image.open(file)"" does not throw an error. I tried everything I could, but alas. A snippet of code if necessary (without trimming options).

filename = askopenfilename()
    for file in filename:
        im = Image.open(file)
        rgb_im = im.convert('RGB')
        rgb_im.save(file.replace("jpg", "png"), quality=95)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-08-28
@glek1

for file in filename:will enumerate in file all characters of the string filename.
You just need to pass your filename to open.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question