使用python,如何阅读文件的"创建日期"?

Syn*_*nia 5 python exif image datecreated

我正在python中编写一个简短的脚本,它将扫描图像文件的文件夹列表,然后重新组织它们.

组织它们的一种可选方式是我们希望它们创建日期.

目前,我正在尝试按如下方式阅读图像创建日期

import os.path, time

f = open("hi.jpg")
data = f.read()
f.close()
print "last modified: %s" % time.ctime(os.path.getmtime(f))
print "created: %s" % time.ctime(os.path.getctime(f))
Run Code Online (Sandbox Code Playgroud)

但是我得到一个错误

Traceback (most recent call last):
  File "TestEXIFread.py", line 6, in <module>
    print "last modified: %s" % time.ctime(os.path.getmtime(f))
  File "/usr/lib/python2.7/genericpath.py", line 54, in getmtime
    return os.stat(filename).st_mtime
TypeError: coercing to Unicode: need string or buffer, file found
Run Code Online (Sandbox Code Playgroud)

谁能告诉我它意味着什么?

Nol*_*lty 8

您需要使用字符串作为文件名而不是文件对象.

>>> import os.path, time
>>> f = open('test.test')
>>> data = f.read()
>>> f.close()
>>> print "last modified: %s" % time.ctime(os.path.getmtime('test.test'))
last modified: Fri Apr 13 20:39:21 2012
>>> print "created : %s" % time.ctime(os.path.getctime('test.test'))
created : Fri Apr 13 20:39:21 2012
Run Code Online (Sandbox Code Playgroud)

  • 注意:`getctime`不会返回创建时间戳:https://docs.python.org/2/library/os.path.html#os.path.getctime (2认同)