I am using Tkinter to create a file manager. So far, I've created a way to browse files, and check files in a folder. My problem is that I am trying to use shutil to move files that have been modified or created in the last 24 hours, from one source folder to a destination folder. My code using python worked fine until I incorporated Tkinter. Now it moves my entire folder, instead of just the files that have been modified in the last 24 hours.
Any help would be appreciated! I am a student, so I am still learning.
from tkinter import *
import shutil
import time
import os
from tkinter import filedialog
root = Tk()
root.title('File Manager')
root.geometry("300x250")
def select_folder():
src = filedialog.askdirectory()
def move_to():
src = filedialog.askdirectory()
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(src):
src_fname = os.path.join(src, fname)
if last_mod_time(src_fname) > before:
dst_fname = os.path.join(dst, fname)
dst = filedialog.askdirectory()
shutil.move(src, dst)
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
select_button = Button(root, text="Select Folder", command= select_folder)
select_button.pack(pady=20)
move_button = Button(root, text="Move To Folder", command= move_to)
move_button.pack(pady=22)
check_button = Button(root, text="File Check", command= file_check)
check_button.pack(pady=24)
root.mainloop()
CodePudding user response:
shutil.move(src, dst)
You told it right there to move the directory. I assume you meant to do
shutil.move(src_fname, dst_fname)
You also have some indentation issues. As a guess you need:
def move_to():
src = filedialog.askdirectory()
dst = filedialog.askdirectory()
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(src):
src_fname = os.path.join(src, fname)
if last_mod_time(src_fname) > before:
dst_fname = os.path.join(dst, fname)
shutil.move(src_fname, dst_fname)