简体   繁体   English

传输新文件或最近 24 小时内编辑过的文件

[英]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.如果它是新文件或在过去 24 小时内编辑过的文件,我试图将某个文件夹中的文件传输到目标文件夹。 Specifically, the transferFiles() function is the part that needs help.具体来说, transferFiles() function 是需要帮助的部分。 I'm still pretty new to Python, any help is greatly appreciated.我对 Python 还是很陌生,非常感谢任何帮助。 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. source_files 只是一个没有 get() 方法的常规列表。 I am not sure what you were trying to do with source_files.get(source_files) .我不确定您尝试使用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.这是一个列表理解简写,用于遍历源文件列表并在每个元素上调用 os.stat() 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 os 目录中获取过去 24 小时内创建的所有文件 - How to get all the files created in last 24 hours in os directory Python:列出最近24小时内创建的具有特定扩展名的子目录中的文件 - Python: List files in subdirectories with specific extension created in last 24 hours 无论如何要在过去 24 小时内修改文件而不遍历目录中的所有文件 - Is there anyway to get files modified in last 24 hours without looping through all files in directory 增强Python脚本以下载在过去24小时内创建的Amazon S3文件 - Enhance Python script to download Amazon S3 files created in last 24 hours 使用 Python,递归地为过去 24 小时内创建的 all.jpg 文件创建符号链接 - Using Python, recursively create symbolic links for all .jpg files created within the last 24 hours 如果在过去24小时内未发送,请发送电子邮件 - Send Email If Not Sent In Last 24 Hours 获取过去 24 小时内创建的记录 - Get records created in last 24 hours Python选择文件夹中最近12小时新生成的文件 - Python select last 12 hours new generated files in a folder 使用过去 24 小时的总和创建新列 - creating new column with the sum of the past 24 hours 让 Telebot 将新用户静音 24 小时 - Make telebot mute new user for 24 hours
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM