TypeError:listdir:path应该是字符串、字节、os.PathLike或None,而不是元组

2024-09-26 22:50:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从一个文件夹中读取多个图像并将其复制到另一个文件夹。我可以选择多个图像,但当我按下文件打开按钮时,会出现上述错误

import shutil,os,glob

from tkinter import filedialog
from tkinter import *
from tkinter.filedialog import askopenfilenames
def callback():
    src = askopenfilenames()
    des = r"C:\Users\tkumar\.spyder-py3\there"
    sourceFiles = os.listdir(src)  #error is here i think.
    #print(len(sourceFiles))
    #shutil.copy(src, des)
    try:
        for fileName in sourceFiles:
            fullName = os.path.join(src, fileName)
            if (os.path.isfile(fullName)):
                shutil.copy(fullName, des)

    except Exception as e:
        print("Error %s" %e)

errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()

Tags: from图像importsrc文件夹ostkintercallback
1条回答
网友
1楼 · 发布于 2024-09-26 22:50:49
# -*- coding: utf-8 -*-
"""
Created on Thu May 14 16:19:47 2020

@author: tkumar
"""
"""
This block of code moves images from one folder(source) to another (destination))
"""
import shutil,os,glob

from tkinter import filedialog
from tkinter import *
from tkinter.filedialog import askopenfilenames
from tkinter.filedialog import askdirectory
def callback():
    #src =''
    tup= askdirectory()
    src= filedialog.askdirectory()
   # src =" ".join(tup) 
    print("      ",type(src))


    #des=''
    des= r"C:\Users\tkumar\.spyder-py3\there"
    sourceFiles = os.listdir(src)
    #print(len(sourceFiles))
    #shutil.copy(src, des)

    try:
        for fileName in sourceFiles:
            fullName = os.path.join(src, fileName)
            if (os.path.isfile(fullName)):
                shutil.copy(fullName, des)

    except Exception as e:
        print("Error %s" %e)

errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop() ```

相关问题 更多 >

    热门问题