0

Newbie question here.

I'm trying the code provided from: How to get file creation & modification date/times in Python?

The exact code I'm running is:

# -*- coding: utf-8 -*-
import os.path, time
myFile = open('IMG_4847.PNG', 'rb')
print "last modified: %s" % time.ctime(os.path.getmtime(myFile))

But, I'm getting the following error:

Traceback (most recent call last):
 File "test.py", line 4, in <module>
   print "last modified: %s" % time.ctime(os.path.getmtime(myFile))
  File "C:\Python27\lib\genericpath.py", line 54, in getmtime
    return os.stat(filename).st_mtime
TypeError: coercing to Unicode: need string or buffer, file found

What am I missing here?

Community
  • 1
  • 1

1 Answers1

1
import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime('IMG_4847.PNG'))

You pass a filepath to getmtime, not a file object.

laike9m
  • 18,344
  • 20
  • 107
  • 140