我想用日期時間戳複製文件。下面的代碼不適用於Windows。我是python的新手,所以請幫助我。python複製帶時間戳的文件
import shutil
import datetime
shutil.copyfile('C:\\Users\\Documents\\error.log','C:\\Users\\Documents\datetime.now().strftime("%Y%m%d-%H%M%S").log')
我想用日期時間戳複製文件。下面的代碼不適用於Windows。我是python的新手,所以請幫助我。python複製帶時間戳的文件
import shutil
import datetime
shutil.copyfile('C:\\Users\\Documents\\error.log','C:\\Users\\Documents\datetime.now().strftime("%Y%m%d-%H%M%S").log')
在您的代碼中,您將代碼包含在字符串中。您需要從字符串中運行代碼,並將其與字符串結合使用。一個解決辦法是
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 忘了第二個日期時間添加到
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')
錯誤Traceback(最近調用最後一次): 文件「checking.py」,第19行,在
我認爲strftime(「%Y%m%d-%H%M%S」)導致問題 – Ajsh
Thank You Robert,偉大的代碼。 – Ajsh
你的意思是保留源文件的時間戳的聲明? – dawg
''C:\\ Users \\ Documents \\ {}。log'.format(datetime.now()。strftime(「%Y%m%d-%H%M%S」))' – MaxU
我想代碼應該複製「error.log」到「error-datetime.log」 – Ajsh