停止shutil.make_archive向自己添加存档 [英] Stop shutil.make_archive adding archive to itself

查看:180
本文介绍了停止shutil.make_archive向自己添加存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Release目录中有App目录

$ cd Release
$ tree
.
`-- App
    |-- App.exe
    ..........

,并且我试图在Release目录中包含所有内容的Release中创建App-1.0.zip.那是在打开App-1.0.zip的包装后,我会得到这个App的目录.

and I am trying to create App-1.0.zip in the Release dir containg App with all its content. That is after unpacking App-1.0.zip I would get this App dir.

我尝试了shutil.make_archive,但是当我这样做时

I tried shutil.make_archive but when I do this

import shutil

shutil.make_archive('App-1.0', 'zip', '.')

Release目录中的

,除了App目录之外,我还在App-1.0.zip中获得了48个字节App-1.0.zip.就是将未完成的存档添加到自身中.

from Release dir, I get 48 byte App-1.0.zip inside App-1.0.zip besides the App dir. That is it adds this unfinished archive to itself.

除了在temp dir中创建档案并移动之外,有什么方法可以避免这种情况?

Is there any way to avoid that except creating the archive in temp dir and moving?

我尝试设置base_dir并将App用作root_dir

shutil.make_archive('App-1.0', 'zip', 'App', 'App')

,但是在设置base_dir时出现找不到App的错误.

but I get error that App is not found when I set base_dir.

Traceback (most recent call last):
  File ".......archive.py", line 4, in <module>
    shutil.make_archive('App-1.0', 'zip', 'App', 'App')
  File "C:\Users\Alex\.virtualenvs\....-nAKWzegL\lib\shutil.py", line 800, in make_archive
    filename = func(base_name, base_dir, **kwargs)
  File "C:\Users\Alex\.virtualenvs\....-nAKWzegL\lib\shutil.py", line 686, in _make_zipfile
    zf.write(path, path)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\Lib\zipfile.py", line 1594, in write
    zinfo = ZipInfo.from_file(filename, arcname)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\Lib\zipfile.py", line 484, in from_file
    st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: "'App'"

'/App''./App'相同.使用完整路径,它可以工作,但是我得到了所有父目录,而不仅仅是App.

The same for '/App' and './App'. With full path it works, but I get all parent dirs, not just App.

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32

推荐答案

以下两种对我有用的解决方案:

Here's a couple of solutions that worked or me:

# curdir: Release
shutil.make_archive('App-1.0', 'zip', '.', 'App')

# curdir: ../Release
shutil.make_archive('Release/App-1.0', 'zip', 'Release', 'App')

这篇关于停止shutil.make_archive向自己添加存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆