我正在学习 python,我正在尝试编写一个愚蠢的代码并卡在这里,我需要像 full-backup-ucs-2013-02-12
#!/usr/bin/python
import os
from time import strftime
DATE=`strftime("%Y-%m-%d")`
backupfile = "full-backup-ucs-" + DATE
print backupfile
当我运行时,我得到以下输出,你看到它在日期中打印两个单引号 ' '
了吗,我想删除它们。我确信有优雅的方法可以做到这一点,请建议:
[spatel@tux work]$ ./backup.py
full-backup-ucs-'2013-02-12'
最佳答案
您可以使用 datetime
模块来获取此信息。
import datetime
DATE = datetime.datetime.now().strftime('%Y-%m-%d')
我相信您已经注意到了,backtic substitution doesn't work in python as it does in the shell .它隐式调用 repr
(在 python2.x 中),这是您附加引号的来源。
编辑 -- 显然你可以只使用删除反义词,你的代码应该或多或少像 time.strftime
一样工作如果省略第二个参数,则使用当前本地时间。
关于Python加入日期和文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839659/