-1

I have a program that I need to copy files from one location to another, then rename the copied file to include a timestamp at the end of the name. So when a file gets copied, the time it was copied gets rewritten to the end of the file name. Currently, my code is a massively roundabout trip to nowhere so I see no point in even posting it. Any ideas? Thank you in advance.

Spenco100
  • 11
  • 1
  • 1
  • Welcome, in SO it is important to always show what you have already written and specify what is not working. This is not a code writing service, it's a place to get help on code you've written. – regina_fallangi Sep 20 '18 at 19:38

2 Answers2

2

To format timestamps you want to look at strftime.

e.g. a = "{}".format(datetime.datetime.now().strftime("%H:%M:%S"))

This will take the string, and format it with a timestamp in the H:M:S format.

jonzlin95
  • 280
  • 2
  • 7
  • I think the more relevant documentation link is this one: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior – Patrick Haugh Sep 20 '18 at 19:29
1

Well you should probably rename the file before you send it over. That way you don't have to deal with finding the new file after you've moved it.

The following python code will take any string and append the current timestamp to it. This timestamp is in ISO format, meaning that it has the following form. YYYY-MM-DD with the hour - minutes - seconds appended to it.

import datetime import time import calendar

def append_timestamp(filename):

     timestamp = calendar.timegm(time.gmtime())
     human_readable = datetime.datetime.fromtimestamp(timestamp).isoformat()
     filename_with_timestamp = filename + "_" + str(human_readable)
     return filename_with_timestamp

print(append_timestamp("FILENAME"))

prints "FILENAME_2018-09-20T12:48:45"