Unter Windows gibt man Pfade für gewöhnlich mit Backslashes an, also etwa "c:\\temp\wichtigerText.txt"
Wenn ich in meinem Python-Programm sowas angebe, dann scheint das nicht immer zu funktionieren, da zumindest "\t" als Tabulator erkannt zu werden scheint.
Gebe ich hingegen an 'c:/(temp/wichtiger.txt' dann scheint das zu funktionieren.
Wie gibt man unter Windows-Python Pfade richtig an.
Ich frage deshalb so blöd, weil ich ein anderes Problem habe, das ich momentan nicht gelöst kriege.
Dateipfade unter Windows/Python
- __blackjack__
- User
- Beiträge: 14330
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Alternativ kann man auch ”rohe” Zeichenkettenliterale verwenden r"c:\\temp\wichtigerText.txt".
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Man kann den Temp Ordner auch auslesen
Code: Alles auswählen
import tempfile
temp_dir = tempfile.gettempdir()
wichtiger_text = f"{temp_dir}/wichtigerText.txt"
print(f"Pfad: {wichtiger_text}")
OS: LMDE5 *** Homepage *** Github Seite
- __blackjack__
- User
- Beiträge: 14330
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Pfade mit Zeichenkettenoperationen zusammensetzen ist eher nicht so toll.
Code: Alles auswählen
from pathlib import Path
from tempfile import gettempdir
file_path = Path(gettempdir()) / "wichtiger_text.txt"
print(f"Pfad: {file_path}")„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
