[英]Transfer files that are new or edited in the last 24 hours
如果它是新文件或在過去 24 小時內編輯過的文件,我試圖將某個文件夾中的文件傳輸到目標文件夾。 具體來說, transferFiles()
function 是需要幫助的部分。 我對 Python 還是很陌生,非常感謝任何幫助。 謝謝!
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()
這是我收到的錯誤:
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 只是一個沒有 get() 方法的常規列表。 我不確定您嘗試使用source_files.get(source_files)
做什么。 但是如果你想得到一個統計列表,做
[os.stat(f) for f in source_files]
這是一個列表理解簡寫,用於遍歷源文件列表並在每個元素上調用 os.stat() 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.