简体   繁体   中英

Transfer files that are new or edited in the last 24 hours

I am trying to have files from a certain folder be transferred to a destination folder if it is a new file or a file that has been edited in the last 24 hours. Specifically, the transferFiles() function is the part that needs help. I'm still pretty new to Python, any help is greatly appreciated. Thanks!

import tkinter as tk
from tkinter import *
import tkinter.filedialog
import os
import time
import shutil
from datetime import datetime, timedelta
import glob



class ParentWindow(Frame):
    def __init__(self, master):
        Frame.__init__(self)
        #title for GUI window
        self.master.title("File Transfer")
        
        #Button to select files from source directory
        self.sourceDir_btn = Button(text="Select Source", width=20, command=self.sourceDir)
        #Positions source button in GUI with tkinter grid()
        self.sourceDir_btn.grid(row=0, column=0, padx=(20,10), pady=(30, 0))
        #Positions source directory selection
        self.source_dir = Entry(width=75)
        #positions entry in GUI using tkinter grid()
        self.source_dir.grid(row=0, column=1, columnspan=2, padx=(20,10), pady=(30, 0))

        #Creates button to select destination of files from destination directory
        self.destDir_btn = Button(text="Select Destination", width=20, command=self.destDir)
        #Positions destination button in GUI using tkinter grid() on the next row
        self.destDir_btn.grid(row=1, column=0, padx=(20, 10), pady=(15, 10))

        #Creates entry for destiantion directory selection
        self.destination_dir = Entry(width=75)
        #Postitions entry in GUI using tkinter grid() padx and pady
        self.destination_dir.grid(row=1, column=1, columnspan=2, padx=(20, 10), pady=(15, 10))

        #creates button to transfer files
        self.transfer_btn = Button(text="Transfer Files", width=20, command = self.transferFiles)
        #positions transfer files button
        self.transfer_btn.grid(row=2, column=1, padx=(200,0), pady=(0,15))

        #creates an exit button
        self.exit_btn = Button(text= "Exit", width=20, comman=self.exit_program)
        #positions the exit button
        self.exit_btn.grid(row=2, column=2, padx=(10, 40), pady=(0, 15))
        
    
    def sourceDir(self):
        selectSourceDir = tkinter.filedialog.askdirectory()
        #The .delete will clear the content that is inserted in the entry widget
        self.source_dir.delete(0, END)
        #The .insert method will insert the user selection to the source_dir Entry
        self.source_dir.insert(0, selectSourceDir)
        

    def destDir(self):
        selectDestDir = tkinter.filedialog.askdirectory()
        #The .delete will clear the content that is inserted in the entry widget
        self.destination_dir.delete(0, END)
        #The insert method will insert the user selection to the destination_dir Entry widget
        self.destination_dir.insert(0,selectDestDir)
        

    def transferFiles(self):
        
        source = self.source_dir.get()

        destination = self.destination_dir.get()

        source_files = os.listdir(source)

        source_files.get(source_files) 

        source_files_return = os.stat(source_files.get())

        for i in source_files:
            modifyDate = datetime.fromtimestamp(os.path.getmtime(source_files_return))
            todaysDate = datetime.today()

            modifyDateLimit = modifyDate + timedelta(days = 1)

            if modifyDateLimit > todaysDate:
                shutil.move(source + '/' + i, destination.get())
                print(i + ' was sucessfully transferred.')

        
        
            
    
    def exit_program(self):
        root.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    App = ParentWindow(root)
    root.mainloop() 

Here is the error I am getting:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\buffy\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\buffy\myRepository\Python-Projects\FileTransferAssignment\file_transfer.py", line 73, in transferFiles
    source_files.get(source_files)
AttributeError: 'list' object has no attribute 'get'

source_files is just a regular list which does not have a get() method. I am not sure what you were trying to do with source_files.get(source_files) . But if you want to get a list of stats, do

[os.stat(f) for f in source_files] 

which is a list comprehension shorthand to iterate through the list of source files and call os.stat() on each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM