我正在尝试使用 python 2.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
我也检查了磁盘空间,这不是问题。我尝试使用触摸手动为“2012 年 11 月 15 日”创建文件,但存档文件也已损坏。其他存档文件非常好,我可以毫无问题地恢复它们。谁能指出这里的问题?