I cannot set ctime/mtime on my file within python. First I get the orginal timestamp of the file through ftp
我不能在python中对我的文件设置ctime/mtime。首先,我通过ftp获得文件的原始时间戳
The only thing I want is to keep the original timestamps on my downloaded files using the ftplib.
我只想使用ftplib保留下载文件的原始时间戳。
def getFileTime(ftp,name):
try :
modifiedTime = ftp.sendcmd('MDTM ' + name)
filtid = datetime.strptime(modifiedTime[4:], "%Y%m%d%H%M%S").strftime("%d %B %Y %H:%M:%S")
return filtid
except :
return False
Then I download the file
然后我下载文件
def downloadFile(ftp, fileName) :
try:
ftp.retrbinary('RETR %s' % fileName,open(fileName, 'wb').write)
except ftplib.error_perm:
print 'ERROR: cannot read file "%s"' % fileName
os.unlink(fileName)
return False
else:
print '*** Downloaded "%s" to CWD' % fileName
return True
and the I want to set the original timestamp to the downloaded file
我想将原始的时间戳设置为下载的文件
def modifyTimestapToOriginal(fileName, orgTime):
#try:
os.utime(fileName, orgTime)
fileName.close()
# return True
# except:
# return False
This is how I am trying to do it
我就是这么做的
ftp, files = f.loginftp(HOST,user,passwd,remoteDir)
for i in files :
if not f.isDir(ftp,i) :
fixTime = datetime.strptime(varfixtime, "%d-%m-%Y %H:%M:%S")
ftime = f.getFileTime(ftp,i)
if ftime >= fixTime :
print (ftime)
os.chdir('c:/testdownload')
f.downloadFile(ftp,i)
settime = ftime.timetuple()
print "settime '%s'" % settime
#f.modifyTimestapToOriginal(i, settime)
The error is :
错误的是:
os.utime(fileName, orgTime)
TypeError: utime() arg 2 must be a tuple (atime, mtime)
Can anyone help me either give me a better way to keep the original file timestamps or how to convert the ftime to a usable tuple for os.utime
谁能帮我一个更好的方法来保存原始的文件时间戳,或者如何将ftime转换为os.utime的一个可用元组
1 个解决方案
#1
6
From the os.utime()
documentation:
从os.utime()文档:
Otherwise, times must be a 2-tuple of numbers, of the form
(atime, mtime)
which is used to set the access and modified times, respectively.否则,时间必须是数字的2元组,其形式(atime, mtime)用于分别设置访问和修改时间。
You are not giving it a tuple. In this case, just set both atime
and mtime
to the same value:
你没有给它一个元组。在这种情况下,只需将atime和mtime设为相同的值:
os.utime(fileName, (orgTime, orgTime))
fileName
is a string, so fileName.close()
won't work (you'll get an attribute error), just drop that line.
文件名是一个字符串,因此fileName.close()不会起作用(您将会得到一个属性错误),只需删除该行。
orgTime
must be an integer; you are giving it a time tuple; convert it to a timestamp in seconds since the epoch with time.mktime()
:
组织时间必须是整数;你给它一个时间元组;将其转换为时间戳,以秒为单位,以秒为单位。
settime = time.mktime(ftime.timetuple())
#1
6
From the os.utime()
documentation:
从os.utime()文档:
Otherwise, times must be a 2-tuple of numbers, of the form
(atime, mtime)
which is used to set the access and modified times, respectively.否则,时间必须是数字的2元组,其形式(atime, mtime)用于分别设置访问和修改时间。
You are not giving it a tuple. In this case, just set both atime
and mtime
to the same value:
你没有给它一个元组。在这种情况下,只需将atime和mtime设为相同的值:
os.utime(fileName, (orgTime, orgTime))
fileName
is a string, so fileName.close()
won't work (you'll get an attribute error), just drop that line.
文件名是一个字符串,因此fileName.close()不会起作用(您将会得到一个属性错误),只需删除该行。
orgTime
must be an integer; you are giving it a time tuple; convert it to a timestamp in seconds since the epoch with time.mktime()
:
组织时间必须是整数;你给它一个时间元组;将其转换为时间戳,以秒为单位,以秒为单位。
settime = time.mktime(ftime.timetuple())