Skip to content
Advertisement

Tk Filedialog giving back a tuple

I am trying to take a file and put it into all directories 1 level down.

TypeError: stat: path should be string, bytes, os.PathLike or integer, not tuple
import shutil
import tkinter as tk
from tkinter import filedialog
import os

root = tk.Tk()
root.withdraw()

files = filedialog.askopenfilenames(parent=root, title='Choose file/s')
path = filedialog.askdirectory()

for dirs in path:
    shutil.copy(files, dirs)

I thought that this would be simple, what am I doing wrong?

Advertisement

Answer

Since it is “askopenfilenames“, it returns the results in the form of a tuple. You can iterate over it and move the files to the directory selected:

import shutil
import tkinter as tk
from tkinter import filedialog
import os

root = tk.Tk()
root.withdraw()

files = filedialog.askopenfilenames(parent=root, title='Choose file/s')
j=filedialog.askdirectory()
path = [os.path.join(j,i) for i in os.listdir(j) if os.path.isdir(os.path.join(j,i))]

for fols,dirs in zip(path,files):
    shutil.copy(dirs, fols)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement