@@ -489,17 +489,8 @@ def delete_file(self, path):
489489 path = path .strip ('/' )
490490 os_path = self ._get_os_path (path )
491491 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 )
503494
504495 if self .delete_to_trash :
505496 self .log .debug ("Sending %s to trash" , os_path )
@@ -510,6 +501,14 @@ def delete_file(self, path):
510501 return
511502
512503 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 )
513512 self .log .debug ("Removing directory %s" , os_path )
514513 with self .perm_to_403 ():
515514 shutil .rmtree (os_path )
0 commit comments