0

我有一个压缩的 .zip 文件,我想将其转换为未压缩的 .tar 文件。

我做了这个功能来做到这一点:

def decompress(filepath):
  devname = re.search("_(.+?)_.+?\.zip", filepath).group(1)
  with zipfile.ZipFile(filepath, 'r') as zip:
    path = os.path.join(start_dir, "Downloads", devname, str(os.path.basename(filepath))[:-4])
    zip.extractall(path)
  with tarfile.open(path + ".tar", 'w') as tar:
    for object in os.listdir(path):
      tar.add(os.path.join(path, object), arcname=object)
  time.sleep(2)
  shutil.rmtree(path, ignore_errors=False, onerror=onError)
  time.sleep(0.5)
  os.remove(filepath)
  return path + ".tar"

运行时出现此错误:

Traceback (most recent call last):
  File "File.py", line 195, in <module>
    main()
  File "File.py", line 184, in main
    dld = download()
  File "File.py", line 132, in download
    filename = decompress(os.path.join(start_dir, "Downloads", devname, filename
))
  File "File.py", line 103, in decompress
    shutil.rmtree(path, ignore_errors=False, onerror=onError)
  File "C:\Program Files (x86)\python27\lib\shutil.py", line 247, in rmtree
    rmtree(fullname, ignore_errors, onerror)
  File "C:\Program Files (x86)\python27\lib\shutil.py", line 247, in rmtree
    rmtree(fullname, ignore_errors, onerror)
  File "C:\Program Files (x86)\python27\lib\shutil.py", line 256, in rmtree
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Program Files (x86)\python27\lib\shutil.py", line 254, in rmtree
    os.rmdir(path)
WindowsError: [Error 145] The directory is not empty: 'C:\\Users\\Vaibhav\\Deskt
op\\Folder\\Downloads\\toro\\_toro_nightly_test\\system\\app'

这是我从https://stackoverflow.com/a/2656405/2518263获得的 onError :

def onError(func, path, exc_info):
    """
    Error handler for ``shutil.rmtree``.

    If the error is due to an access error (read only file)
    it attempts to add write permission and then retries.

    If the error is for another reason it re-raises the error.

    Usage : ``shutil.rmtree(path, onerror=onerror)``
    """
    if not os.access(path, os.W_OK):
        # Is the error an access error ?
        os.chmod(path, stat.S_IWUSR)
        func(path)
    else:
        raise

我只在随机场合得到错误。解压缩功能在 75% 的时间内工作。

我不知道为什么我会收到这个错误。有人可以提出更好的方法或解决此错误的方法。


我刚换了:

shutil.rmtree(path, ignore_errors=False, onerror=onError)

和:

for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        filename = os.path.join(root, name)
        os.chmod(filename, stat.S_IWUSR)
        os.remove(filename)
    for name in dirs:
        os.rmdir(os.path.join(root, name))
  time.sleep(0.5)
  os.rmdir(path)

现在它就像一个魅力。

编辑:没关系!我仍然会收到错误,但很少见,现在大约有 20% 的时间!请帮忙!

编辑2:

好的,所以如果我提取到一个临时文件,我不会得到错误,所以我使用这个代码:

import tempfile
def decompress(filepath):
  with zipfile.ZipFile(filepath) as zip:
    tmp = tempfile.mkdtemp(dir=os.getcwd())
    zip.extractall(tmp)
  with tarfile.open(filepath[:-4] + ".tar", 'w') as tar:
    for object in os.listdir(tmp):
      tar.add(os.path.join(tmp, object), arcname=object)
  time.sleep(1)
  shutil.rmtree(tmp)
  os.remove(filepath)
  return filepath[:-4] + ".tar"

我现在工作!(希望错误不会再次发生)

编辑 3:我再次收到错误!!!!!!!!!!这真的让我很紧张。请帮助某人。

4

1 回答 1

1

看起来您的操作系统中的某个进程设法在您正在删除的目录中创建另一个文件。

我可以建议您避免创建临时文件并将原始 ZIP 的解压缩部分直接提供到新的 TAR 文件中。

这需要将 ZipInfo 字段与 TarInfo 匹配,但它应该很简单。

这是我的看法:

def zip2tar(zipname, tarname):
    zipf = zipfile.ZipFile(zipname, 'r')
    tarf = tarfile.TarFile(tarname, 'w')
    timeshift = int((datetime.datetime.now() -
                     datetime.datetime.utcnow()).total_seconds())
    for zipinfo in zipf.infolist():
        tarinfo = tarfile.TarInfo()
        tarinfo.name = zipinfo.filename
        tarinfo.size = zipinfo.file_size
        tarinfo.mtime = calendar.timegm(zipinfo.date_time) - timeshift
        if zipinfo.internal_attr & 1:
            tarinfo.mode = 0666
            tarinfo.type = tarfile.REGTYPE
        else:
            tarinfo.mode = 0777
            tarinfo.type = tarfile.DIRTYPE 
        infile = zipf.open(zipinfo.filename)
        tarf.addfile(tarinfo, infile)
    zipf.close()
    tarf.close()
于 2013-08-25T19:44:57.203 回答