久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python 轉(zhuǎn)換日期時間以在 os.utime 中使用

Python converting datetime to be used in os.utime(Python 轉(zhuǎn)換日期時間以在 os.utime 中使用)
本文介紹了Python 轉(zhuǎn)換日期時間以在 os.utime 中使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我無法在 Python 中對我的文件設(shè)置 ctime/mtime.首先我通過 FTP 獲取文件的原始時間戳.

I cannot set ctime/mtime on my file within Python. First I get the original timestamp of the file through FTP.

我唯一想要的就是使用 ftplib 在我下載的文件上保留原始時間戳.

The only thing I want is to keep the original timestamps on my downloaded files using the 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

然后我下載文件

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

             

我想為下載的文件設(shè)置原始時間戳

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

    

這就是我嘗試的方式

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)

                 
    

錯誤是:

    os.utime(fileName, orgTime)
TypeError: utime() arg 2 must be a tuple (atime, mtime)

誰能幫我給我一個更好的方法來保留原始文件時間戳或如何將 ftime 轉(zhuǎn)換為 os.utime 的可用元組

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

推薦答案

來自 os.utime() 文檔:

From the os.utime() documentation:

否則,times 必須是數(shù)字的二元組,格式為 (atime, mtime),分別用于設(shè)置訪問時間和修改時間.

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.

你沒有給它一個元組.在這種情況下,只需將 atimemtime 都設(shè)置為相同的值:

You are not giving it a tuple. In this case, just set both atime and mtime to the same value:

os.utime(fileName, (orgTime, orgTime))

fileName 是一個字符串,所以 fileName.close() 不起作用(你會得到一個屬性錯誤),只需刪除該行.

fileName is a string, so fileName.close() won't work (you'll get an attribute error), just drop that line.

orgTime 必須是整數(shù);你給它一個時間元組;使用 time.mktime 將其轉(zhuǎn)換為以秒為單位的時間戳():

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())

這篇關(guān)于Python 轉(zhuǎn)換日期時間以在 os.utime 中使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標(biāo))
主站蜘蛛池模板: 特级一级黄色片 | 中文字幕在线观看国产 | 黑人巨大精品欧美黑白配亚洲 | 韩日免费视频 | 久久精品二区亚洲w码 | 日韩精品在线一区 | 九九热在线观看视频 | 免费在线一区二区 | 亚洲欧美少妇 | 国产日韩精品在线 | 一级黄色毛片免费 | 亚洲一区二区在线 | 一级黄色毛片子 | 久久久国产一区二区三区 | 国产sm主人调教女m视频 | 久久久久久亚洲 | 一区二区三区在线播放 | 中文字幕在线视频免费视频 | 久草色视频 | 欧美日韩一区二区三区四区五区 | 在线视频99 | 中文字幕视频免费 | 在线观看视频亚洲 | 青春草91 | 九九热在线免费视频 | 狠狠入ady亚洲精品经典电影 | 91精品国产一区二区三区 | 欧美aa在线| 国产精品海角社区在线观看 | 精品成人av | 丁香婷婷综合激情五月色 | 色约约视频 | 7777在线视频免费播放 | 少妇黄色 | 一区二区三区视频在线免费观看 | 亚洲国产欧美在线 | 中文字幕在线观看一区 | 亚洲综合日韩精品欧美综合区 | 欧美久久久电影 | 一区二区三区视频在线观看 | 亚洲成人中文字幕 |