Seite 1 von 1

Ganzen Pfad löschen

Verfasst: Freitag 2. Januar 2009, 18:08
von INFACT
Oder eine andere Möglichkeit einen ganzen Pfad zu löschen indem man die Dateien einzeln löscht funktioniert nicht ganz:

Code: Alles auswählen

import os
def PfadLoeschen():
	for g in os.walk("C:Neuer Ordner"):
		import os.path
		anfang="C:Neuer Ordner"
		Ende=g
		alles(anfang+Ende)
		if os.path.isfile(alles):
			os.remove(alles)
	removedirs("C:Neuer Ordner")
Könntet ihr mir ne kleine Hilfe Geben???

Falls ja, danke

Verfasst: Freitag 2. Januar 2009, 18:18
von cofi
os.removedirs
os.remove(path)¶
Remove the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use. Availability: Unix, Windows.

os.removedirs(path)¶

Remove directories recursively. Works like rmdir() except that, if the leaf directory is successfully removed, removedirs() tries to successively remove every parent directory mentioned in path until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, os.removedirs('foo/bar/baz') will first remove the directory 'foo/bar/baz', and then remove 'foo/bar' and 'foo' if they are empty. Raises OSError if the leaf directory could not be successfully removed.

New in version 1.5.2.
Wenn die Ordner nicht leer sind, musst du die natürlich leeren.

Verfasst: Freitag 2. Januar 2009, 18:18
von snafu

Code: Alles auswählen

shutil.rmtree(dein_pfad)

Verfasst: Freitag 31. Juli 2009, 10:38
von daRonny
Genau das habe ich gesucht um meinen ganzen Pfad zu löschen. Die Methode zum selber durchsuchen dauert viel zu lange.
Super.

Verfasst: Freitag 31. Juli 2009, 12:14
von HWK
INFACT == daRonny ?

Verfasst: Freitag 31. Juli 2009, 14:55
von derdon
INFACT: Eine import-Anweisung innerhalb einer Schleife ist übrigens keine gute idee.