pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

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

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

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

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è)置原始時(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)

                 
    

錯(cuò)誤是:

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

誰能幫我給我一個(gè)更好的方法來保留原始文件時(shí)間戳或如何將 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è)置訪問時(shí)間和修改時(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.

你沒有給它一個(gè)元組.在這種情況下,只需將 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 是一個(gè)字符串,所以 fileName.close() 不起作用(你會得到一個(gè)屬性錯(cuò)誤),只需刪除該行.

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

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

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)換日期時(shí)間以在 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))
主站蜘蛛池模板: 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂 | 物流公司电话|附近物流公司电话上门取货 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 云阳人才网_云阳招聘网_云阳人才市场_云阳人事人才网_云阳人家招聘网_云阳最新招聘信息 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 私人别墅家庭影院系统_家庭影院音响_家庭影院装修设计公司-邦牛影音 | FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 四探针电阻率测试仪-振实密度仪-粉末流动性测定仪-宁波瑞柯微智能 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 薄壁轴承-等截面薄壁轴承生产厂家-洛阳薄壁精密轴承有限公司 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | 灰板纸、灰底白、硬纸板等纸品生产商-金泊纸业 | 日本SMC气缸接头-速度控制阀-日本三菱伺服电机-苏州禾力自动化科技有限公司 | 楼承板-钢筋楼承板-闭口楼承板-无锡优贝斯楼承板厂 | 120kv/2mA直流高压发生器-60kv/2mA-30kva/50kv工频耐压试验装置-旭明电工 | 托利多电子平台秤-高精度接线盒-托利多高精度电子秤|百科 | 智慧农业|农业物联网|现代农业物联网-托普云农物联网官方网站 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 泥沙分离_泥沙分离设备_泥砂分离机_洛阳隆中重工机械有限公司 | 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 |