Ajsh Ajsh - 2 months ago 19
Python Question

python copy files with time stamp

I would like to copy file with date time stamp. the below code is not working on windows. I am new to python so please help me.

import shutil
import datetime
shutil.copyfile('C:\\Users\\Documents\\error.log','C:\\Users\\Documents\datetime.now().strftime("%Y%m%d-%H%M%S").log')

Answer

In your code, you have the code enclosed in the string. You need to run the code out of the string, and combine it with the string. A solution would be

import shutil
import datetime
shutil.copyfile('C:\\Users\\Documents\\error.log','C:\\Users\\Documents\' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.log')

UPDATE Forgot to add the second datetime to the statement

import shutil
import datetime
shutil.copyfile('C:\\Users\\Documents\\error.log','C:\\Users\\Documents\' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '.log')
Comments