import os
import time
import send2trash
import datetime
from tkinter import filedialog
from tkinter import *
import tkinter as tk
root = tk.Tk()
date = input("Enter your date: ")
date1 = time.strptime(date, '%Y-%m-%d')
date2 = time.mktime(date1)
dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
for f in os.listdir(dirname):
modified_time = os.path.getmtime(f)
if modified_time < date2:
send2trash.send2trash(f)
并且有一个错误:
File "C:\Users\FotoHub\Desktop\test0109.py", line 20, **in <module>
modified_time = os.path.getmtime(f)**
File "C:\Users\FotoHub\AppData\Local\Programs\Python\Python37-32\Lib\genericpath.py", line 55, **in getmtime
return os.stat(filename).st_mtime**
**builtins.FileNotFoundError: [WinError 2] The system cannot find the file specified: '322097.jpg'**
答案 0 :(得分:0)
As documented,os.listdir()
仅返回文件名称。要统计文件(或实际执行其他任何操作),您必须使用os.path.join()
重建完整路径:
for filename in os.listdir(dirname):
filepath = os.path.join(dirname, filename)
modified_time = os.path.getmtime(filepath)