Python Time Modification Help

3295
14
12-27-2019 08:39 AM
SamLee1
New Contributor

Hello,

When I run my python script, it downloads a file from a website to a directory and it moves an old file from that directory to an archived folder and renames it with filename+date moved/date when script runs.

I am trying to fix it to make it show filename+modified/created date instead of filename+date moved/date when script runs.

This file is in the archived folder by the way.

Attached is the python code.

Tags (2)
0 Kudos
14 Replies
RandyBurton
MVP Alum

To change to a 4 digit year, just use an upper case "Y" in line 41:

date1 = date1.strftime('_%Y-%m-%d')

See strftime and strptime behavior for the datetime format codes.

0 Kudos
SamLee1
New Contributor

Sweet worked. 

Can I basically use that link to change the formatting of the date to any combination of date formats?

For example, change it to 12-30-2019 and etc.?

Also, how can I mark this discussion/post as done/correct?

0 Kudos
RandyBurton
MVP Alum

The link should show you all the formatting codes related to the timedate object.  To display the date with month first and year last, just move %Y to the end.

To mark a correct answer, you would need to change this "discussion" to a "question".  Then a correct answer option would be available.

0 Kudos
RandyBurton
MVP Alum

Since you are interested in file creation/modification dates,  I was experimenting with the following:

from datetime import datetime
import dateutil.tz as tz  # to convert utc to local time
import dateutil.parser as parser # will parse a variety of datetime formats
import requests
import  os.path
import xml.etree.ElementTree as ET # for parsing xml file
import zipfile # to open zip file (excel uses this format)

directory = r'C:\Path\To\Save\Directory' # directory for saved file
file = 'Grid_Support_Inverter_List_Full_Data.xlsm'  # name of saved file

# file to download
url = "https://www.gosolarcalifornia.ca.gov/equipment/documents/Grid_Support_Inverter_List_Full_Data.xlsm"
content = requests.get(url)
# print content.headers # for debugging

# get Last-Modified date from header and print as a local time
webModDate = parser.parse(content.headers['Last-Modified']).astimezone(tz.tzlocal())
print "Web Last-Modified: {}".format(webModDate.strftime('%Y-%m-%d %H:%M:%S'))

# save downloaded excel file to local drive
with open(os.path.join(directory, file), 'wb') as f:
    f.write(content.content)
f.close()

# excel file format is a type of zip file
# read an xml file inside the excel file to get file modified date
# and convert to local date timetime
zip = zipfile.ZipFile(os.path.join(directory, file))
props = zip.open('docProps/core.xml') # core document properties
xmlText = props.read()
zip.close()
# print xmlText # for debugging

ns = { 'cp' : 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
       'dc' : 'http://purl.org/dc/elements/1.1/',
       'dcterms' : 'http://purl.org/dc/terms/' }

tree = ET.fromstring(xmlText)

creator =  tree.find('dc:creator', ns).text
lastModBy =  tree.find('cp:lastModifiedBy', ns).text
created =  tree.find('dcterms:created', ns).text
modified = tree.find('dcterms:modified', ns).text
# print creator, lastModBy, created, modified # for debugging

# convert excel modified date to local datetime
xlModDate = parser.parse(modified).astimezone(tz.tzlocal())
print "Excel file modified: {}".format(xlModDate.strftime('%Y-%m-%d %H:%M:%S'))

# get the saved file's create and modified date from operating system
# datetime will be timestamp in local time, when file was saved
# (basically the time when this script was run)
osCreateDate = datetime.fromtimestamp(os.path.getctime(os.path.join(directory, file)))
print "OS file created: {}".format(osCreateDate.strftime('%Y-%m-%d %H:%M:%S'))
osModDate = datetime.fromtimestamp(os.path.getmtime(os.path.join(directory, file)))
print "OS file modified: {}".format(osModDate.strftime('%Y-%m-%d %H:%M:%S'))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The results:

Web Last-Modified: 2020-01-02 10:48:55
Excel file modified: 2020-01-02 09:32:50
OS file created: 2020-01-03 18:12:12
OS file modified: 2020-01-03 18:12:12

From this, you can see that the Excel file was modified a bit over an hour before it was put on the web server.  The modified date/time came directly from xml inside the Excel file.

The operating system gave the file the same create and modified date indicating when the file was downloaded and saved to the local system (and not the actual time the Excel file was last modified).

You should be able to use requests.head(url) to retrieve just the header to examine the modified date and then determine if you want to actually download the file.

0 Kudos
SamLee1
New Contributor

Thank you for further information and coding about this, very beneficial!

0 Kudos