在Python中将DATE添加到文件名

时间:2013-12-06 09:41:33

标签: python json datetime

Lo All, 首先,大多数人可能会认为这是一个重复的帖子,但它有一个独特的元素:tar ... 我现在已经发布了几次这个代码,很多都有帮助。但是,我修改了代码以将日期添加到文件名。不幸的是,输出只是没有改变......代码是:

import time
import datetime
import os
import json
import tarfile
source_config='/myapp/brendan.json'
backup_dir = '/myapp/target/'
DATE_FORMAT = '%Y%m%d'
now = time.strftime(DATE_FORMAT) 
timestr_now = time.strftime('%Y%m%d')

def main():
    print timestr_now
    with open(source_config) as f:
                data = json.load(f)
                for entry in data["source_include"]:
                    base_filename = os.path.basename(entry)
                    #source_dirs = [ name for name in os.listdir(entry) if os.path.isdir(os.path.join(entry, name)) ]
                    full_dir = os.path.join(entry)
                    testname = (timestr_now, backup_dir, base_filename)
                    date_name = str(timestr_now + "_" + base_filename)
                    tar = tarfile.open(os.path.join(backup_dir, date_name + '.tar.gzip'), 'w:gz')
                    tar.add(full_dir)
                    tar.close()

和JSON:

{
    "source_type": "folder",
    "tar_type": "gzip",
    "tar_max_age": "10",
    "source_include": ["/myapp/conf/", 
                        "/myapp/db/", 
                        "/myapp/randomdata/"],
    "target_path": "/myapp/target/",
    "del_age": "10"
}

请注意,JSON中的其他选项将在以后使用。

因此,当我运行此代码时,它返回日期:20131206(在控制台中),这是打印时间线的结果。然而,产生的焦油被命名为conf.tar.gzip和db.tar.gzip ......而不是预期的20131206conf.tar.gzip ......不知道我到底做错了什么?

0 个答案:

没有答案
相关问题