【问题标题】:python copy files with time stamppython复制带有时间戳的文件
【发布时间】:2017-01-13 06:07:57
【问题描述】:

我想复制带有日期时间戳的文件。下面的代码在 Windows 上不起作用。我是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')

【问题讨论】:

  • 你的意思是保留源文件的时间戳?
  • 'C:\\Users\\Documents\\{}.log'.format(datetime.now().strftime("%Y%m%d-%H%M%S"))
  • 我希望代码应该将“error.log”复制到“error-datetime.log”

标签: python shutil file-copying


【解决方案1】:

在您的代码中,您将代码包含在字符串中。您需要从字符串中运行代码,并将其与字符串结合。一个解决方案是

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.datetime.now().strftime("%Y%m%d-%H%M%S") + '.log')

【讨论】:

  • 错误回溯(最近一次调用最后一次):文件“checking.py”,第 19 行,在 shutil.copyfile('C:\\Users\\Documents\\error.log' ,'C:\\Users\\Documents\\' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.txt') AttributeError: 'module'对象没有“现在”属性
  • 我认为 strftime("%Y%m%d-%H%M%S") 引起了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 2015-09-12
相关资源
最近更新 更多