2009-10-12 11 views

Antwort

44

Verwendung shutil.rmtree:

import shutil 

shutil.rmtree(path) 

Siehe the documentation für Details, wie zu handhaben und/oder Fehler ignorieren.

+13

Dies schlägt für mich fehl, wenn es Dateien im Verzeichnis gibt. Sieh dghubbles Beitrag. – CornSmith

7

Sie wollen shutil.rmtree

shutil.rmtree(path[, ignore_errors[, onerror]])

Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

32

Die Standardbibliothek enthält shutil.rmtree dafür. Standardmäßig wird

shutil.rmtree(path) # errors if dir not empty 

geben OSError: [Errno 66] Directory not empty: <your/path> geben.

Sie können das Verzeichnis und dessen Inhalt sowieso löschen, indem Sie den Fehler zu ignorieren:

shutil.rmtree(role_fs_path, ignore_errors=True) 

Sie auch anspruchsvollere Fehlerbehandlung durchführen kann durch onerrror=<some function(function, path, excinfo)> vorbei.

+4

'ignore_errors = True 'bedeutet, dass das Verzeichnis nicht entfernt wird. – ostrokach

+0

ignore_errors = True war der Tickket –

+0

Es funktioniert für mich. – Jerome

Verwandte Themen