1

I am relatively new to Python and im trying to make a script that finds files (photos) that have been created between two dates and puts them into a folder. For that, I need to get the creation date of the files somehow (Im on Windows). I already have everything coded but I just need to get the date of each picture. Would also be interesting to see in which form the date is returned. The best would be like m/d/y or d/m/y (d=day; m=month, y=year). Thank you all in advance! I am new to this forum

4 Answers4

1

I imagine you are somehow listing files if so then use the os.stat(path).st_ctime to get the creation time in Windows and then using datetime module string format it.

https://docs.python.org/2/library/stat.html#stat.ST_CTIME

https://stackoverflow.com/a/39359270/928680 this example shows how to convert the mtime (modified) time but the same applies to the ctime (creation time)

once you have the ctime it's relatively simple to check if that falls with in a range

https://stackoverflow.com/a/5464465/928680

you will need to do your date logic before converting​ to a string.

one of the solutions, not very efficient.. just to show one of the ways this can be done.

import os
from datetime import datetime

def filter_files(path, start_date, end_date, date_format="%Y"):
    result = []
    start_time_obj = datetime.strptime(start_date, date_format)
    end_time_obj = datetime.strptime(end_date, date_format)
    for file in os.listdir(path):
        c_time = datetime.fromtimestamp(os.stat(file).st_ctime)
        if start_time_obj <= c_time <= end_time_obj:
            result.append("{}, {}".format(os.path.join(path, file), c_time))
    return result


if __name__ == "__main__":
    print "\n".join(filter_files("/Users/Jagadish/Desktop", "2017-05-31", "2017-06-02", "%Y-%m-%d"))

cheers!

Jag
  • 51
  • 8
  • Thank you very much! Im testing this later. Also: is there an "easy" way to get all files within a folder to THEN search for the ones created within a specified timespan?:) – crazycoder69 Jun 01 '17 at 18:42
  • i've added a example to the answer, which filters files between 2 dates – Jag Jun 02 '17 at 07:21
0

See the Python os package for basic system commands, including directory listings with options. You'll be able to extract the file date. See the Python datetime package for date manipulation.

Also, check the available Windows commands on your version: most of them have search functions with a date parameter; you could simply have an OS system command return the needed file names.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

You can use subprocess to run a shell command on a file to get meta_data of that file.

import re
from subprocess import check_output

meta_data = check_output('wmic datafile where Name="C:\\\\Users\\\\username\\\\Pictures\\\\xyz.jpg"', shell=True)

# Note that you have to use '\\\\' instead of '\\' for specifying path of the file

pattern = re.compile(r'\b(\d{14})\b.*')

re.findall(pattern,meta_data.decode())

=> ['20161007174858'] # This is the created date of your file in format - YYYYMMDDHHMMSS
Amey Dahale
  • 750
  • 6
  • 10
0

Here is my solution. The Pillow/Image module can access the metadata of the .png file. Then we access the 36867 position of that metadata which is DateTimeOriginal. Finally I convert the string returned to a datetime object which gives flexibility to do whatever you need to do with it. Here is the code.

from PIL import Image
from datetime import datetime

# Get the creationTime
creationTime = Image.open('myImage.PNG')._getexif()[36867]

# Convert creationTime to datetime object
creationTime = datetime.strptime(creationTime, '%Y:%m:%d %H:%M:%S')
Casivio
  • 333
  • 7
  • 15