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()
„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