File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change 9
9
# module imports
10
10
""" Utilities to load and save image objects """
11
11
12
- import os . path as op
12
+ import os
13
13
import numpy as np
14
14
15
15
from .filename_parser import splitext_addext
@@ -36,9 +36,11 @@ def load(filename, **kwargs):
36
36
img : ``SpatialImage``
37
37
Image of guessed type
38
38
'''
39
- if not op .exists (filename ):
40
- raise FileNotFoundError ("No such file: '%s'" % filename )
41
- if op .getsize (filename ) <= 0 :
39
+ try :
40
+ stat_result = os .stat (filename )
41
+ except OSError :
42
+ raise FileNotFoundError ("No such file or no access: '%s'" % filename )
43
+ if stat_result .st_size <= 0 :
42
44
raise ImageFileError ("Empty file: '%s'" % filename )
43
45
sniff = None
44
46
for image_klass in all_image_classes :
Original file line number Diff line number Diff line change @@ -58,6 +58,14 @@ def test_file_not_found():
58
58
assert_raises (FileNotFoundError , load , 'does_not_exist.nii.gz' )
59
59
60
60
61
+ def test_load_empty_image ():
62
+ with InTemporaryDirectory ():
63
+ open ('empty.nii' , 'w' ).close ()
64
+ with assert_raises (ImageFileError ) as err :
65
+ load ('empty.nii' )
66
+ assert_true (err .exception .args [0 ].startswith ('Empty file: ' ))
67
+
68
+
61
69
def test_read_img_data_nifti ():
62
70
shape = (2 , 3 , 4 )
63
71
data = np .random .normal (size = shape )
You can’t perform that action at this time.
0 commit comments