Wobei `filename` besser `path` hiesse, denn vor dem ``if`` ist noch nicht klar ob es ein *Datei*name ist. Stem + Suffix ist umständlich für `name` und `filename.name` sieht irgendwie komisch aus. Zudem hat `Path` mit `with_name()` eine Methode um einen Pfad mit einem anderen Namen zu erstellen was sich ganz nett liest.
#!/usr/bin/env python3
from pathlib import Path
QUELLPFAD = Path("E:/Fileordner")
def main():
if not QUELLPFAD.exists():
print("Fehler Pfad existiert nicht")
else:
print("Pfad gefunden")
for path in QUELLPFAD.iterdir():
print(path)
if path.is_file():
path.rename(path.with_name("text" + path.name))
if __name__ == "__main__":
main()
“Design patterns are kind of like sarcasm: hard to use well, not always appropriate,
and disgustingly bad when applied to problems they are not meant to solve.” — ajones in c.l.python