我一直收到这个错误。任何洞察力都会有所帮助。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\14065\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\14065\Documents\GitHub\Python-Projects\FILE_TRANSFER_PART_3.py", line 70, in move_to
for fname in os.listdir(sourceLocation):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not StringVar#importing packages
from tkinter import *
import shutil
import time
import os
from tkinter import filedialog
root = Tk()
root.title('File Manager')
root.geometry("600x200")
#tkinter widgets
def create_widgets():
link_label = Label(root, text = "Select Files To Copy:")
link_label.grid(row = 1, column = 0,
pady = 5, padx = 5)
root.sourceText = Entry(root, width = 50,
textvariable = sourceLocation)
root.sourceText.grid(row = 1, column = 1,
pady = 5, padx = 5,
columnspan = 2)
source_browsebtn = Button(root,text = "Browse",
command = SourceBrowse, width = 15)
source_browsebtn.grid(row = 1, column = 3,
pady = 5, padx = 5)
destinationLabel = Label(root, text = "Select The Destination:")
destinationLabel.grid(row = 2, column = 0,
pady = 5, padx = 5)
root.destinationText = Entry(root, width = 50,
textvariable = destinationLocation)
root.destinationText.grid(row = 2, column = 1,
pady = 5, padx = 5,
columnspan = 2)
dest_browsebtn = Button(root, text ="Browse",
command = DestinationBrowse, width = 15)
dest_browsebtn.grid(row = 2, column = 3,
pady = 5, padx = 5)
move_button = Button(root, text="Move Files", command= move_to, width = 15)
move_button.grid(row = 3, column = 1,
pady = 5, padx = 5)
check_button = Button(root, text="File Check", command= file_check, width = 15)
check_button.grid(row = 3, column = 2,
pady = 5, padx = 5)
def SourceBrowse():
root.files_list = list(filedialog.askdirectory())
root.sourceText.insert('1', root.files_list)
def DestinationBrowse():
destinationdirectory = filedialog.askdirectory()
root.destinationText.insert('1', destinationdirectory)
def move_to():
file_list = root.files_list
destination_location = destinationLocation.get()
SECONDS_IN_DAY = 24 * 60 * 60
now = time.time()
before = now - SECONDS_IN_DAY
def last_mod_time(fname):
return os.path.getmtime(fname)
for fname in os.listdir(sourceLocation):
src_fname = os.path.join(sourceLocation, fname)
if last_mod_time(src_fname) > before:
dst_fname = os.path.join(destinationLocation, fname)
shutil.move(src_fname, dst_fname)
def file_check():
folderList = filedialog.askdirectory()
sortlist = sorted(os.listdir(folderList))
i=0
print("Files in ", folderList, "folder are:")
while(i<len(sortlist)):
print(sortlist[i]+'\n')
i+=1
sourceLocation= StringVar()
destinationLocation= StringVar()
create_widgets()
root.mainloop()发布于 2021-10-30 13:46:37
正如@acw1668在注释中所述,您应该尝试os.listdir(sourceLocation.get()),因为这会将StringVar类型对象转换为可用作os.listdir函数中的参数的string。
https://stackoverflow.com/questions/69718023
复制相似问题