1
- import bz2
2
1
from contextlib import contextmanager
3
- import gzip
4
2
import os
5
3
from shutil import rmtree
6
4
import tempfile
7
- import zipfile
8
5
9
- from pandas .compat import get_lzma_file , import_lzma
10
-
11
- lzma = import_lzma ()
6
+ from pandas .io .common import get_handle
12
7
13
8
14
9
@contextmanager
@@ -28,36 +23,8 @@ def decompress_file(path, compression):
28
23
-------
29
24
file object
30
25
"""
31
- if compression is None :
32
- f = open (path , "rb" )
33
- elif compression == "gzip" :
34
- # pandas\_testing.py:243: error: Incompatible types in assignment
35
- # (expression has type "IO[Any]", variable has type "BinaryIO")
36
- f = gzip .open (path , "rb" ) # type: ignore[assignment]
37
- elif compression == "bz2" :
38
- # pandas\_testing.py:245: error: Incompatible types in assignment
39
- # (expression has type "BZ2File", variable has type "BinaryIO")
40
- f = bz2 .BZ2File (path , "rb" ) # type: ignore[assignment]
41
- elif compression == "xz" :
42
- f = get_lzma_file (lzma )(path , "rb" )
43
- elif compression == "zip" :
44
- zip_file = zipfile .ZipFile (path )
45
- zip_names = zip_file .namelist ()
46
- if len (zip_names ) == 1 :
47
- # pandas\_testing.py:252: error: Incompatible types in assignment
48
- # (expression has type "IO[bytes]", variable has type "BinaryIO")
49
- f = zip_file .open (zip_names .pop ()) # type: ignore[assignment]
50
- else :
51
- raise ValueError (f"ZIP file { path } error. Only one file per ZIP." )
52
- else :
53
- raise ValueError (f"Unrecognized compression type: { compression } " )
54
-
55
- try :
56
- yield f
57
- finally :
58
- f .close ()
59
- if compression == "zip" :
60
- zip_file .close ()
26
+ with get_handle (path , "rb" , compression = compression , is_text = False ) as handle :
27
+ yield handle .handle
61
28
62
29
63
30
@contextmanager
0 commit comments