Why the extract function stopped extracting?
Can someone explain and help me resolve why my function stopped extracting .tgz files when I added a counter to create folders with different names to keep the extracted folder from overwriting the previous one when I extracted another .tgz file in the same directory? What am I doing wrong? Thanks! Below are the two functions ... the first function extracts files properly; the second function extracts a numbered folder and quits.
Works:
def extract(tar_url, extract_path='.'):
print tar_url
tar = tarfile.open(tar_url, 'r')
for item in tar:
tar.extract(item, extract_path)
if item.name.find(".tgz") != -1 or item.name.find(".tar") != -1:
extract(item.name, "./" + item.开发者_开发问答name[:item.name.rfind('/')])
Does not work:
global counter
counter=1
def extract(tar_url, extract_path='.'):
global counter
print tar_url
tar = tarfile.open(tar_url, 'wb')# changed from r to wb 6/12
for item in tar:
tar.extract(item, extract_path+"_%d"%counter)
counter+=1
if item.name.find(".tgz") != -1 or item.name.find(".tar") != -1:
extract(item.name, "./" + item.name[:item.name.rfind('/')])
Here is how I call it in main (I'm using easygui):
direct = diropenbox(msg="Choose path to place extracted files!", title='SQA Extractor', default='c:\\Extracted')
msg = "Are you sure you want to extract?"
title = "Confirm"
os.chdir(direct)
try:
for root, dirname, files in os.walk(directory):
for file1 in files:
if file1.endswith(".tgz") or file1.endswith(".tar"):
extract(os.path.join(root, file1))
Perhaps it was this change that broke your code:
tar = tarfile.open(tar_url, 'r')
Changed to:
tar = tarfile.open(tar_url, 'wb')# changed from r to wb 6/12
Does the extract path with the counter exist?
for item in tar:
os.mkdir(extract_path + "_%d" % counter)
tar.extract(item, extract_path+"_%d" % counter)
counter+=1
if item.name.find(".tgz") != -1 or item.name.find(".tar") != -1:
extract(item.name, "./" + item.name[:item.name.rfind('/')])
The original version relies on the created folder names matching the relative paths specified in the archive. In the new version, the recursive call tries to put the files into a folder without a 'tag' number, after extracting the other files at that level into one that does.
Try adding the tag to the path name used for the recursive call as well.
BTW, the Python-idiomatic spelling of item.name.find(".tar") != -1
is '.tar' in item.name
.
精彩评论