我已经在论坛上搜索了这类错误,但没有找到任何错误,所以我正在为它创建新的主题。我正在用下面的python脚本归档一年前的文件。
import os, time, tarfile
path = "/home/appins/.scripts/test/"
now = time.time()
yearago = now - 60*60*24*665
tar_file = "nas_archive_"+time.strftime("%m-%d-%Y")+".tgz"
tar = tarfile.open(tar_file,"w:gz")
for root, subFolders, files in os.walk(path):
for file in files:
file = os.path.join(root,file)
file = os.path.join(path, file)
filecreation = os.path.getctime(file)
if filecreation > yearago:
tar.add(file)
print file," is older that one year"
os.remove(file)它工作得很好,我可以通过查看它的内容。现在,当我尝试恢复存档文件时,收到错误AttributeError:'TarFile‘对象没有'endswith’属性。
我的恢复脚本很简单:
import os, tarfile
archive_file = "nas_archive_07-31-2013.tgz"
tar = tarfile.open("nas_archive_07-31-2013.tgz")
tar.extractall(tar)
tar.close()当我运行这个脚本时,我得到了下面的错误:
python restore_archive.py
Traceback (most recent call last):
File "restore_archive.py", line 8, in ?
tar.extractall(tar)
File "/usr/lib64/python2.4/tarfile.py", line 1541, in extractall
self.extract(tarinfo, path)
File "/usr/lib64/python2.4/tarfile.py", line 1578, in extract
self._extract_member(tarinfo, os.path.join(path, tarinfo.name))
File "/usr/lib64/python2.4/posixpath.py", line 62, in join
elif path == '' or path.endswith('/'):
AttributeError: 'TarFile' object has no attribute 'endswith'在提取过程中有什么地方我做错了吗?我可以使用tar -xzvf命令解压缩文件。
发布于 2013-07-31 23:44:02
extractall方法接受要提取到的路径。我不知道为什么要将tar文件对象传递给它;您应该能够省略path参数,并将其缺省为当前目录。
发布于 2013-07-31 23:45:09
In [96]: help(tarfile.TarFile.extractall)
Help on function extractall in module tarfile:
extractall(self, path='.', members=None)
Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
to extract to. `members' is optional and must be a subset of the
list returned by getmembers().
(END)因此,extractall需要一个路径(str对象)作为第一个参数。
https://stackoverflow.com/questions/17974745
复制相似问题