python skript
-
Hallo ich versuche grad ein python-skript zu schreiben dass mir alle .mp3 .ogg usw auf der konsole ausgibt, die ich in einem bestimmten ordner habe (rekursiver durchlauf)
das bisherige sieht so aus:
import os import sys sys.setrecursionlimit(20000000) print("Starte") musikendungen = [".ogg", ".mp3", ".flac", ".wma"] def find_musik(path): for useless, dirs, files in os.walk(path): for dir in dirs: find_musik(dir) for file in files: if os.path.splitext(file)[1] in musikendungen: print(files) find_musik("C:\\")leider funktioniert das nicht, nach einer weile heißt es einfach : python.exe funktioniert nicht mehr...
Ich verwende python3.1
-
os.walk() ist doch schon rekursiv. Also lösche einfach Zeile 12 und 13. Hättest du gesehen wenn du "files" einfach mal ausgegeben hättest, bevor du die Funktion nochmal aufrufst.
-
hmmm.. da hätte ich selbst drauf kommen können

allerdings:
ich hatte am anfang versucht mit listdir zu arbeiten, das at aber nicht geklappt weil ich nicht weiß wie ich
die strings dann als files interpretieren kann...also so in etwa:
for file in os.listdir(path): if os.path.isdir(file): rekursion(file) ...allerdings kann os.path.isdir nicht ausgeführt werden, type(file) spuckt string aus
-
Wieso kann os.path.isdir nicht ausgeführt werden? Die Funktion erwartet einen String.
from os import listdir, getcwd from os.path import isdir def listfiles(path): for file in listdir(path): if isdir(file): listfiles(path + "\\" + file) else: print(path + "\\" + file) listfiles(getcwd())Funktioniert wunderbar.
-
nwp2 schrieb:
from os import listdir, getcwd from os.path import isdir def listfiles(path): for file in listdir(path): if isdir(file): listfiles(path + "\\" + file) else: print(path + "\\" + file) listfiles(getcwd())Ein echter nwp2. Das ist Mist:
$ pwd /root/eins $ python eins.py /root/eins\eins.py Traceback (most recent call last): File "eins.py", line 10, in <module> listfiles(getcwd()) File "eins.py", line 7, in listfiles listfiles(path + "\\" + file) File "eins.py", line 5, in listfiles for file in listdir(path): OSError: [Errno 2] No such file or directory: '/root/eins\\zwei'Wenn schon die Pfade selbst bauen, dann lieber so:
from os import listdir, getcwd from os.path import isdir, sep def listfiles(path): for file in listdir(path): if isdir(file): listfiles(path + sep + file) else: print(path + sep + file) listfiles(getcwd())
-
Da der Threadersteller
find_musik("C:\\")benutzt funktioniert es bei ihm. Außerdem liefere ich keine Komplettlösung, sondern einen Ansatz, dass es nicht perfekt ist ist klar.
Ich hab auch Klammern um print gemacht, zuviel C.
-
nwp2 schrieb:
Ich hab auch Klammern um print gemacht, zuviel C.
Wie schon shisha. print() ist seit Jahren eine Funktion.

-
ich sag zu den klammern nur: python 3.x