-1

I would like to save in a variable the current time (I can do this) I then want to copy file a to file b and include the timestamp in file b. like this copy file1 file1.08-02-2019 (like a backup of the file) I can copy the file fine except when I add the timestamp?

I can do the copy with no issues. I just can't get the copy to work with the timestamp variable in the dest file name

import shutil
import datetime
import os

now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H:%M:%S"))

os.system('copy C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl, C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl_backup'+'timestamp')

it takes the time stamp as literal meaning the word timestamp is in the dest file name not the actual value of the timestamp variable

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
kevin wholley
  • 85
  • 1
  • 8
  • In windows no `:` is allowed as part of the filename, only as drive-delimiter ... – Patrick Artner Aug 03 '19 at 17:59
  • You add the text `'timestamp'` to your filename - not your variables content – Patrick Artner Aug 03 '19 at 18:00
  • @PatrickArtner I think you have mistakenly marked this as duplicate. Please read the question carefully. "*I can do the copy with no issues. I just can't get the copy to work with the timestamp variable in the dest file name*". So referring posts on `how to rename a file` or `how to copy a file` in python does not do the justice to OP. Kindly, remove the duplicate marker. – fiveelements Aug 03 '19 at 18:37
  • @five I added some more duplicates - there are about 5 to 10 others floating around with answers. Probably closest to this users problem (after fixing using the _variable_ instead of a string of the variable-name is [Adding timestamp to a file in PYTHON](https://stackoverflow.com/questions/49736080/adding-timestamp-to-a-file-in-python). All solve the underlying problem by telling him how to fix his code to create a file with a timestamp in it. If not a duplicate this would probably be a "Close due to typo" kind of question that should not have answers at all... – Patrick Artner Aug 03 '19 at 19:29

2 Answers2

0

You can't have colons in filenames. Use some other delimeter, like a period. Also C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl_backup'+'timestamp'

timestamp shouldn't be in quotes, as this makes it a string literal.

clubby789
  • 2,543
  • 4
  • 16
  • 32
0

You could use something like the below. Make sure to format your timestamp appropriately so it can be supported in the filename by OS.

import shutil 
import datetime 
import os

now = datetime.datetime.now() timestamp =
str(now.strftime("%Y_%m_%d_%H_%M_%S"))

src = "C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl"
dest = "C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl_backup_"+timestamp
shutil.copy(src, dest)

Or the last 3 lines could be replaced by the way you are doing (I am not sure though whether it actually copies):

os.system('copy C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl, C:\\Users\\kwhol\\my_python_code\\twp_pid_ctrl_backup_'+timestamp)
fiveelements
  • 3,649
  • 1
  • 17
  • 16