Python 日期的Tar文件已损坏

Python 日期的Tar文件已损坏,python,Python,我正在尝试使用Python2.6从文件服务器归档旧文件。我正在根据日期创建tar文件。下面是我的代码: #/usr/bin/python import os, time, tarfile from datetime import datetime, date, timedelta import datetime path = "/nfs4exports/opt/prod/fmac/files/inbound/test_archive" count = 0 set_date = '2010-1

我正在尝试使用Python2.6从文件服务器归档旧文件。我正在根据日期创建tar文件。下面是我的代码:

#/usr/bin/python

import os, time, tarfile
from datetime import datetime, date, timedelta
import datetime

path = "/nfs4exports/opt/prod/fmac/files/inbound/test_archive"
count = 0
set_date = '2010-12-17'

base_date = datetime.datetime.strptime(set_date, '%Y-%m-%d')

# Creating dictionary for files to be archived
date_file_dict = {}
for root, subFolders, files in os.walk(path):
        for file in files:
            file = os.path.join(root,file)
            file = os.path.join(path, file)
            stats = os.stat(file)
            c_date = date.fromtimestamp(stats[8]).strftime('%m-%d-%y')
            date_file_tuple = c_date, file
            date_file_dict[file] = c_date

d_list = date_file_dict.values()
dd_list = list(set(d_list))
date_occur_dict = {}

# Adding files to archive and generating log
for search_date in dd_list:
    tar_file = "nas_archive_" + search_date + ".tgz"
    mytar = tarfile.open(tar_file,"w:gz")
    log_file = "archive_log_" + search_date
    fcount = 0
    #print tar_file
    #print log_file
    f = open(log_file,'ab+')
    for f_name, d_date in date_file_dict.iteritems():
        if d_date == search_date:
            fcount += 1
            mytar.add(f_name)
            f.write(f_name + '\n')
    date_occur_dict[search_date] = fcount

# Checking before removing files
for check_count in dd_list:
    if d_list.count(check_count) == date_occur_dict.get(check_count):
        for f_name, d_date in date_file_dict.iteritems():
            if d_date == check_count:
                os.remove(f_name)
除了日期11-15-12之外,上述代码工作正常,并正确生成归档文件和日志。每次运行脚本时都会生成归档文件“nas_Archive_11-15-12.tgz”,但不知何故会损坏

tar -tzvf nas_archive_11-15-12.tgz
-rw-r--r-- appins/app     1961 2012-11-15 15:09:56 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_379a6265-a121-4040-8014-babe397e98ed.txt.gpg
-rw-r--r-- appins/app     2337 2012-11-15 10:25:09 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_839c5bc5-a6b8-4fef-bb26-e1d8e91b3f84.txt.gpg

gzip: -rw-r--r-- appins/app     5102 2012-11-15 15:04:33 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_5c8b315b-7b99-42d3-9cc7-235b72ab9176.txt.gpg
stdin: unexpected end of file
-rw-r--r-- appins/app     1445 2012-11-15 10:19:06 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Output_ad548dea-374c-47c8-a6f4-a6780af0ab43.csv.gpg
-rw-r--r-- appins/app     1344 2012-11-15 15:13:48 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Output_66d20187-1e99-438d-9860-73d0fa6ead04.csv.gpg
-rw-r--r-- appins/app     2635 2012-11-15 16:09:51 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Output_894a2cd2-1e39-46a9-b805-201b5d430181.csv.gpg
-rw-r--r-- appins/app     3997 2012-11-15 15:02:32 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_6104d8b7-47fd-4d49-aa7e-1e0fc404fbcb.txt.gpg
-rw-r--r-- appins/app     2603 2012-11-15 09:46:29 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_e23cf5f5-11de-45c5-a2d3-2403a28068fe.txt.gpg
-rw-r--r-- appins/app     2325 2012-11-15 14:37:06 nfs4exports/opt/prod/fmac/files/inbound/test_archive/Input_882db028-1a06-422f-9192-fe3fc58e2e3f.txt.gpg
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now

我也检查了磁盘空间,这不是问题。我试图使用touch为“2012年11月15日”手动创建文件,但同时该文件的存档文件已损坏。其他存档文件非常好,我可以毫无问题地恢复它们。有人能指出这里的问题吗?

复制我的评论作为答案:


也许可以尝试在mytar上添加一个明确的结尾。它可能无法正确终止存档。

可能尝试在
mytar
上添加一个显式关闭。它可能没有正确终止存档。公牛眼!!!!。它起作用了。@user2501825请注意,Python 2.6(及更高版本)有
with
语句,它可以帮助您实现这一点。如果你养成了始终如一地使用它的习惯,你将永远不会忘记一个
关闭