Python: Temporäres Verzeichnis erstellen
English
Deutsch
Hinweis: Dieses Beispiel zeigt, wie man ein temporäres Verzeichnis erstellt, das nicht automatisch gelöscht wird. Siehe Selbstlöschendes temporäres Verzeichnis in Python erstellen für ein Beispiel, wie man ein selbstlöschendes temporäres Verzeichnis erstellt!
Minimales Beispiel:
tempdir_example.py
import tempfile
tempdir = tempfile.mkdtemp()
print(tempdir) # gibt z.B. /tmp/tmpvw0936nd austempfile.mkdtemp() erstellt dieses Verzeichnis automatisch. Das Verzeichnis wird nicht automatisch gelöscht!
Benutzerdefiniertes Präfix (empfohlen):
tempdir_custom_prefix.py
import tempfile
tempdir = tempfile.mkdtemp(prefix="myapplication-")
print(tempdir) # gibt z.B. /tmp/myapplication-ztcy6s2w ausVerzeichnis löschen
Um das temporäre Verzeichnis inklusive aller Dateien darin zu löschen, verwenden Sie
tempdir_delete.py
import shutil
shutil.rmtree(tempdir)Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow