1

I am trying to extract all available date and timestamps for all files in the CWD.

For example YYYYMMDD_HHMMSS (even if the output not so nice):

lastwritetime     creationtime      datetakentime
20171124_190646   20171124_190646   20171124_190646   file1.txt
20171124_190646   20171124_190646   20171124_190646   file2.txt
20171124_190646   20171124_190646   20171124_190646   file3.txt

I am getting syntax error for (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file). Any hints what I am doing wrong?

Code:

import os
import time
cwd = os.getcwd()
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
timestr = time.strftime("%Y%m%d_%H%M%S")
files = open(cwd)
files = [f for f in os.listdir(cwd) if os.path.isfile(f)]
for f in files:
    print("created: %s" % time.ctime(os.path.getctime(file) timestr))
    print("last modified: %s" % time.mtime(os.path.getmtime(file) timestr))
HakariDo
  • 277
  • 1
  • 12

2 Answers2

1
    print("created: %s" % time.ctime(os.path.getctime(file) timestr))

You would need a , comma if you wanted timestr to be the 2nd argument to ctime. But it only takes one argument.

There is a difference between a scalar numeric value representing seconds since 1970, and a datetime. It looks like you want to hand the getctime() result to utcfromtimestamp(). Then you'll have an object you can call strftime() on, which will accept timestr as an argument.

J_H
  • 17,926
  • 4
  • 24
  • 44
1

I got the same errors that you did. First, I had to move line 4 after "file" is defined, or, in this case, "f". Then, the second error was the syntax of the print command. If you place a comma before timestr, it should print fine. The third problem was that I was getting permission denied, apparently from trying to open a whole directory.

I have rewritten some of your script to something that I think better suits your needs.

import glob
import time
import os

timestr = time.strftime("%Y%m%d_%H%M%S")
files = glob.glob('*.*')

for file in files:
    print('created: %s' % time.ctime(os.path.getctime(file)), timestr)
    print('last modified: %s' % time.ctime(os.path.getmtime(file)), timestr)

However, seeing your first posted example, I thought I could modify this to match it. Here's what I came up with:

import glob
import time
import os

files = glob.glob('*.*')

print('    created           modified        file_name')
for file in files:
    print('%s' % time.strftime("%Y%m%d_%H%M%S", time.gmtime(os.path.getctime(file))), end='   ')
    print('%s' % time.strftime("%Y%m%d_%H%M%S", time.gmtime(os.path.getmtime(file))), end='   ')
    print(file)

Running it gave me:

    created           modified        file_name
20170723_223307   20170723_223307   36e73b41-4366-4bf8-825f-9f16e41b1b41.tmp
20160928_232029   20171116_081039   desktop.ini
20171124_215147   20171124_215413   help.py
20171124_215637   20171124_221613   help2.py
20171124_214353   20171124_214353   test.txt

Now, it's just a matter of you getting the parameters on the right places and configuring the print commands to suit your exact needs.

Edit: With the request to extract EXIF data, the code looks like this. Forgive the blank except.

import glob
import time
import os
from PIL import Image

files = glob.glob('*.*')

print('      created               modified                EXIF            file_name')
for file in files:
    print('%s' % time.strftime("%Y:%m:%d %H:%M:%S", time.gmtime(os.path.getctime(file))), end='   ')
    print('%s' % time.strftime("%Y:%m:%d %H:%M:%S", time.gmtime(os.path.getmtime(file))), end='   ')
    try:
        print('%s' % Image.open(file)._getexif()[36867], end='   ')
    except:
        print('Not an image', end='          ')
    print(file)

Remember that you can adjust glob to only get jpg, png, or whatever you want. This script gets everything and if it's not an image, it just prints that it failed.

K.Cl
  • 1,615
  • 2
  • 9
  • 18
  • very nice!!! thank you! a bonus question if i may: how would you insert, if available for the file, the EXIF date and time taken? for example: [https://stackoverflow.com/questions/23064549](https://stackoverflow.com/questions/23064549/get-date-and-time-when-photo-was-taken-from-exif-data-using-pil). i literally would like to extract all available times for the files to see which one is really the true "creation" time. – HakariDo Nov 24 '17 at 22:34
  • 1
    Your link helped a lot. I would add the following line: print('%s' % Image.open(file)._getexif()[36867], end=' '). Enclose it in a try loop for more safety. I've updated the comment with this new addition. – K.Cl Nov 24 '17 at 22:59