@@ -489,17 +489,8 @@ def delete_file(self, path):
489
489
path = path .strip ('/' )
490
490
os_path = self ._get_os_path (path )
491
491
rm = os .unlink
492
- if os .path .isdir (os_path ):
493
- listing = os .listdir (os_path )
494
- # Don't delete non-empty directories.
495
- # A directory containing only leftover checkpoints is
496
- # considered empty.
497
- cp_dir = getattr (self .checkpoints , 'checkpoint_dir' , None )
498
- for entry in listing :
499
- if entry != cp_dir :
500
- raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
501
- elif not os .path .isfile (os_path ):
502
- raise web .HTTPError (404 , u'File does not exist: %s' % os_path )
492
+ if not os .path .exists (os_path ):
493
+ raise web .HTTPError (404 , u'File or directory does not exist: %s' % os_path )
503
494
504
495
if self .delete_to_trash :
505
496
self .log .debug ("Sending %s to trash" , os_path )
@@ -510,6 +501,14 @@ def delete_file(self, path):
510
501
return
511
502
512
503
if os .path .isdir (os_path ):
504
+ listing = os .listdir (os_path )
505
+ # Don't permanently delete non-empty directories.
506
+ # A directory containing only leftover checkpoints is
507
+ # considered empty.
508
+ cp_dir = getattr (self .checkpoints , 'checkpoint_dir' , None )
509
+ for entry in listing :
510
+ if entry != cp_dir :
511
+ raise web .HTTPError (400 , u'Directory %s not empty' % os_path )
513
512
self .log .debug ("Removing directory %s" , os_path )
514
513
with self .perm_to_403 ():
515
514
shutil .rmtree (os_path )
0 commit comments