[파이썬] shutil `shutil.get_unpack_formats()`: 지원되는 압축 해제 포맷 목록 가져오기

In Python, the shutil module provides a collection of high-level operations on files and directories. One useful function in this module is shutil.get_unpack_formats(). This function returns a list of supported unpacking formats for various compressed file types.

Usage

To use shutil.get_unpack_formats(), you need to import the shutil module:

import shutil

Then, you can call the get_unpack_formats() function to retrieve the supported unpacking formats:

formats = shutil.get_unpack_formats()

Retrieving the supported unpacking formats

The get_unpack_formats() function returns a list of tuples, where each tuple contains the name of the format and a description. You can iterate over this list to access the individual format details:

formats = shutil.get_unpack_formats()

for format_name, description in formats:
    print("Format:", format_name)
    print("Description:", description)
    print()

Example Output

Here is an example output of calling shutil.get_unpack_formats():

Format: zip Description: ZIP archive

Format: gztar Description: gzip’ed tar-file

Format: bztar Description: bzip2’ed tar-file

Format: tar Description: uncompressed tar file

The above output shows four supported unpacking formats with their corresponding descriptions.

Conclusion

shutil.get_unpack_formats() is a handy function provided by the shutil module in Python. It allows you to retrieve a list of supported unpacking formats for compressed files. By using this function, you can easily determine which formats are compatible with your application and handle them accordingly.