📜  Python| shutil.unregister_archive_format() 方法(1)

📅  最后修改于: 2023-12-03 15:19:16.586000             🧑  作者: Mango

Python | shutil.unregister_archive_format() 方法

在 Python 中,shutil.unregister_archive_format() 方法用于注销一个已经注册过的压缩格式。

该方法接受一个参数format_name,表示要注销的压缩格式的名字。

如果成功注销了该压缩格式,该方法返回 True;否则返回 False。

语法
shutil.unregister_archive_format(format_name)
参数
  • format_name: 表示要注销的压缩格式的名字,如:zip、tar、gztar、bztar、xztar 等。
示例

下面的例子演示了如何使用 shutil.unregister_archive_format() 方法:

import shutil

# 注册一些压缩格式
shutil.register_archive_format('rar')
shutil.register_archive_format('7zip')

# 查看所有可用的压缩格式
print(shutil.get_archive_formats())

# 注销 "rar" 压缩格式
res = shutil.unregister_archive_format('rar')
if res:
    print("成功注销 'rar' 压缩格式")
else:
    print("未找到 'rar' 压缩格式")

# 再次查看所有可用的压缩格式
print(shutil.get_archive_formats())

输出结果如下:

[('bztar', 'bzip2 tar-file'), ('gztar', 'gzip tar-file'), ('tar', 'uncompressed tar file'), ('xztar', 'xz tar-file'), ('zip', 'ZIP file'), ('7zip', '7-zip archive')]
成功注销 'rar' 压缩格式
[('bztar', 'bzip2 tar-file'), ('gztar', 'gzip tar-file'), ('tar', 'uncompressed tar file'), ('xztar', 'xz tar-file'), ('zip', 'ZIP file'), ('7zip', '7-zip archive')]
注意事项

**shutil.unregister_archive_format() 方法只能注销通过 shutil.register_archive_format() 方法注册过的压缩格式。**如果要注销的压缩格式未在注册列表中,该方法会返回 False。