如果我正确地做这个部分,请告诉我。我试图抓取过去24小时内仅修改过的文件。但是,无论修改时间如何,我的输出都是目录中的所有文件:
yesterday = date.today() - timedelta(days=1)
dayToStr = yesterday.strftime('%Y%m%d')
file_list_attr = sftp.listdir_attr()
for file in file_list_attr:
if file.st_mtime <= dayToStr:
print file
输出
-rw-r - r-- 1 4012 60 3404961 09 Jan 18:32 2_YEAR_912828UD0_20130109.dat -rw-r - r-- 1 4012 60 10206411 09 Jan 18:32 3_YEAR_912828UG3_20130109.dat -rw-r - r-- 1 4012 60 68311760 09 Jan 18:34 5_YEAR_912828UE8_20130109.dat -rw-r - r-- 1 4012 60 54215712 09 Jan 18:35 7_YEAR_912828UF5_20130109.dat -rw-r - r-- 1 4012 60 88014103 09 Jan 18:37 10_YEAR_912828TY6_20130109.dat -rw-r - r-- 1 4012 60 53565072 09 Jan 18:38 30_YEAR_912810QY7_20130109.dat -rw-r - r-- 1 4012 60 8527412 04 Jan 18:31 2_YEAR_912828UD0_20130104.dat -rw-r - r-- 1 4012 60 21659138 04 Jan 18:31 3_YEAR_912828UC2_20130104.dat -rw-r - r-- 1 4012 60 91281894 04 Jan 18:34 5_YEAR_912828UE8_20130104.dat -rw-r - r-- 1 4012 60 80421507 04 Jan 18:36 7_YEAR_912828UF5_20130104.dat -rw-r - r-- 1 4012 60 108700356 04 Jan 18:38 10_YEAR_912828TY6_20130104.dat -rw-r - r-- 1 4012 60 50204292 04 Jan 18:39 30_YEAR_912810QY7_20130104.dat -rw-r - r-- 1 4012 60 2319656 07 Jan 18:24 2_YEAR_912828UD0_20130107.dat -rw-r - r-- 1 4012 60 6978760 07 Jan 18:24 3_YEAR_912828UC2_20130107.dat -rw-r - r-- 1 4012 60 53579177 07 Jan 18:25 5_YEAR_912828UE8_20130107.dat -rw-r - r-- 1 4012 60 46069381 07 Jan 18:26 7_YEAR_912828UF5_20130107.dat -rw-r - r-- 1 4012 60 70802355 07 Jan 18:28 10_YEAR_912828TY6_20130107.dat -rw-r - r-- 1 4012 60 43050822 07 Jan 18:29 30_YEAR_912810QY7_20130107.dat -rw-r - r-- 1 4012 60 2713906 08 Jan 18:31 2_YEAR_912828UD0_20130108.dat -rw-r - r-- 1 4012 60 8889264 08 Jan 18:31 3_YEAR_912828UC2_20130108.dat -rw-r - r-- 1 4012 60 63857903 08 Jan 18:32 5_YEAR_912828UE8_20130108.dat -rw-r - r-- 1 4012 60 55544096 08 Jan 18:34 7_YEAR_912828UF5_20130108.dat -rw-r - r-- 1 4012 60 89750161 08 Jan 18:36 10_YEAR_912828TY6_20130108.dat -rw-r - r-- 1 4012 60 59233399 08 Jan 18:37 30_YEAR_912810QY7_20130108.dat
答案 0 :(得分:3)
file.st_mtime
是整数时间戳。
dayToStr
是一个字符串。
在Python2中,integers
总是比较strings
,而i
来自int
之前的s
。 {1}} str
按字母顺序排列:
In [123]: 1234 < 'foobar'
Out[123]: True
在Python3中,将int
与str
进行比较会引发TypeError:
>>> 1234 < 'foobar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
相反,比较日期时间对象:
import datetime as DT
import os
yesterday = DT.datetime.now() - DT.timedelta(days=1)
# or, if you want 00:00 AM, yesterday:
# yesterday = DT.datetime.now().replace(hour = 0, minute = 0, second = 0, microsecond = 0) - DT.timedelta(days=1)
file_list_attr = sftp.listdir_attr()
for pfile in file_list_attr:
if DT.datetime.fromtimestamp(pfile.st_mtime) > yesterday:
print pfile
参考文献:
DT.datetime
对象。yesterday
)设置为零。答案 1 :(得分:0)
与'昨天'比较时似乎失败
for pfile in file_list_attr:
print DT.datetime.fromtimestamp(pfile.st_mtime)
2013-01-09 18:32:06
2013-01-09 18:32:22
2013-01-09 18:34:07
2013-01-09 18:35:27
2013-01-09 18:37:38
for pfile in file_list_attr:
print DT.datetime.fromtimestamp(pfile.st_mtime) > yesterday
Traceback (most recent call last):
File "<pyshell#41>", line 2, in <module>
print DT.datetime.fromtimestamp(pfile.st_mtime) > yesterday
TypeError: can't compare datetime.datetime to datetime.date
答案 2 :(得分:0)
这是一个如何做的例子:
打印所有符合24小时前修改过的条件的文件
# Task: grab files ONLY modified in the past 24 hours
import os
import datetime
myPath = "/users/george/documents/"
# Adding all the files found in myFolder in a collection
fileCollection = os.listdir(myPath)
# Iterating through the files, printing their last modified date
for i in fileCollection:
# Getting the timestamp in a variable
fileModTimeStamp = os.path.getmtime(myPath + str(i))
fileModDateTime = datetime.datetime.fromtimestamp(fileModTimeStamp)
# Calculating the time delta
currentTime = datetime.datetime.now()
timeElapsed = currentTime - fileModDateTime
# 24h dimedelta
twentyFourHours = datetime.datetime(1900, 1, 2, 0, 0, 0, 0) - datetime.datetime(1900, 1, 1, 0, 0, 0, 0)
# Print the files that meet the condition
if timeElapsed <= twentyFourHours:
print "The File: " + str(i) + " Was Last Modified At: " + str(fileModDateTime) + " ,Which was about: " \
+ str(timeElapsed) + " ago."
答案 3 :(得分:-1)
我不相信os模块会工作,因为我使用paramiko来SFTP到远程主机并对目录中的文件执行操作
for filename in file_list_attr:
mtime = os.path.getmtime(filename)
print mtime
Traceback (most recent call last):
File "<pyshell#22>", line 2, in <module>
mtime = os.path.getmtime(filename)
File "U:\ActivPy\lib\genericpath.py", line 54, in getmtime
return os.stat(filename).st_mtime
TypeError: coercing to Unicode: need string or buffer, SFTPAttributes found