How to copy and save an image into a new folder?

Question:

So far, I’ve only managed to prompt the user for an image:

def addImage(event=None):
    fileName = filedialog.askopenfilename() 
    return fileName

As far as I know, this only returns the path of the image. How would I be able to let the user choose an image from their personal drive and then save it into a new folder?

Asked By: rjrj

||

Answers:

You can use the shutil module to move or copy files to different locations

import shutil

destination = "/some/destination/path"

def addImage(event=None):
    fileName = filedialog.askopenfilename() 
    shutil.copy(fileName, destination)
    return fileName

shutil also has functions for moving files instead of copying them, and functions for copying or moving full directories as well.

shutil docs

Answered By: Alexander
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.