python converting datetime to be used in os.utime

后端 未结 1 1317
广开言路
广开言路 2021-01-04 10:13

I cannot set ctime/mtime on my file within python. First I get the orginal timestamp of the file through ftp

The only thing I want is to keep the original timestamp

相关标签:
1条回答
  • 2021-01-04 11:07

    From the os.utime() documentation:

    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.

    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 is a string, so fileName.close() won't work (you'll get an attribute error), just drop that line.

    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())
    
    0 讨论(0)
提交回复
热议问题