|
1 | 1 | ==============================
|
2 |
| -13.8 创建和解压压缩文件 |
| 2 | +13.8 创建和解压归档文件 |
3 | 3 | ==============================
|
4 | 4 |
|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You need to create or unpack archives in common formats (e.g., .tar, .tgz, or .zip). |
| 8 | +你需要创建或解压常见格式的归档文件(比如.tar, .tgz或.zip) |
9 | 9 |
|
10 | 10 | |
|
11 | 11 |
|
12 | 12 | ----------
|
13 | 13 | 解决方案
|
14 | 14 | ----------
|
15 |
| -The shutil module has two functions—make_archive() and unpack_archive()—that |
16 |
| -do exactly what you want. For example: |
| 15 | +``shutil`` 模块拥有两个函数—— ``make_archive()`` 和 ``unpack_archive()`` 可派上用场。 |
| 16 | +例如: |
17 | 17 |
|
18 |
| ->>> import shutil |
19 |
| ->>> shutil.unpack_archive('Python-3.3.0.tgz') |
| 18 | +.. code-block:: python |
20 | 19 |
|
21 |
| ->>> shutil.make_archive('py33','zip','Python-3.3.0') |
22 |
| -'/Users/beazley/Downloads/py33.zip' |
23 |
| ->>> |
| 20 | + >>> import shutil |
| 21 | + >>> shutil.unpack_archive('Python-3.3.0.tgz') |
24 | 22 |
|
25 |
| -The second argument to make_archive() is the desired output format. To get a list of |
26 |
| -supported archive formats, use get_archive_formats(). For example: |
| 23 | + >>> shutil.make_archive('py33','zip','Python-3.3.0') |
| 24 | + '/Users/beazley/Downloads/py33.zip' |
| 25 | + >>> |
27 | 26 |
|
28 |
| ->>> shutil.get_archive_formats() |
29 |
| -[('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), |
30 |
| - ('tar', 'uncompressed tar file'), ('zip', 'ZIP file')] |
31 |
| ->>> |
| 27 | +``make_archive()`` 的第二个参数是期望的输出格式。 |
| 28 | +可以使用 ``get_archive_formats()`` 获取所有支持的归档格式列表。例如: |
| 29 | + |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + >>> shutil.get_archive_formats() |
| 33 | + [('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), |
| 34 | + ('tar', 'uncompressed tar file'), ('zip', 'ZIP file')] |
| 35 | + >>> |
32 | 36 |
|
33 | 37 | |
|
34 | 38 |
|
35 | 39 | ----------
|
36 | 40 | 讨论
|
37 | 41 | ----------
|
38 |
| -Python has other library modules for dealing with the low-level details of various archive |
39 |
| -formats (e.g., tarfile, zipfile, gzip, bz2, etc.). However, if all you’re trying to do is |
40 |
| -make or extract an archive, there’s really no need to go so low level. You can just use |
41 |
| -these high-level functions in shutil instead. |
42 |
| -The functions have a variety of additional options for logging, dryruns, file permissions, |
43 |
| -and so forth. Consult the shutil library documentation for further details. |
| 42 | +Python还有其他的模块可用来处理多种归档格式(比如tarfile, zipfile, gzip, bz2)的底层细节。 |
| 43 | +不过,如果你仅仅只是要创建或提取某个归档,就没有必要使用底层库了。 |
| 44 | +可以直接使用 ``shutil`` 中的这些高层函数。 |
| 45 | + |
| 46 | +这些函数还有很多其他选项,用于日志打印、预检、文件权限等等。 |
| 47 | +参考 `shutil文档 <https://docs.python.org/3/library/shutil.html>`_ |
0 commit comments