删除 Y 文件夹中早于 X 天的每个文件
Delete each file older than X days in Y folder
我写了这个但是它不起作用。
在 giorni
中,我将最长停留天数放在 SD 中,file_dir
是分析文件的默认位置。
import os
from datetime import datetime, timedelta
file_dir = "/home/pi/" #location
giorni = 2 #n max of days
giorni_pass = datetime.now() - timedelta(giorni)
for root, dirs, files in os.walk(file_dir):
for file in files:
filetime = datetime.fromtimestamp(os.path.getctime(file))
if filetime > giorni_pass:
os.remove(file)
解决方法:
for file in files:
path = os.path.join(file_dir, file)
filetime = datetime.fromtimestamp(os.path.getctime(path))
if filetime > giorni_pass:
os.remove(path)
因为"Filenames"包含一个文件列表,其路径名是相对于"file_dir"的,要对这些文件进行操作应该首先获取绝对路径,使用path = os.path.join(file_dir, file)
我写了这个但是它不起作用。
在 giorni
中,我将最长停留天数放在 SD 中,file_dir
是分析文件的默认位置。
import os
from datetime import datetime, timedelta
file_dir = "/home/pi/" #location
giorni = 2 #n max of days
giorni_pass = datetime.now() - timedelta(giorni)
for root, dirs, files in os.walk(file_dir):
for file in files:
filetime = datetime.fromtimestamp(os.path.getctime(file))
if filetime > giorni_pass:
os.remove(file)
解决方法:
for file in files:
path = os.path.join(file_dir, file)
filetime = datetime.fromtimestamp(os.path.getctime(path))
if filetime > giorni_pass:
os.remove(path)
因为"Filenames"包含一个文件列表,其路径名是相对于"file_dir"的,要对这些文件进行操作应该首先获取绝对路径,使用path = os.path.join(file_dir, file)