[Python] python os.renames create chaotic results

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    So I want to rename mp3 Files using their own Meta Data and sort them out (with/without metadata) and get sketchy results and a weird FileNotFound Error while he does the statement (picture follows)

    sketchy Result -> one of 2 songs is renamed properly, but the other one gets their own title split while one part is part of the name and the other part is kinda a new subDirectory!?

    Error -> FileNotFoundError: [WinError 2]

    The python code, it's really short

    print("Start")

    tracks = []
    tracksMeta = []
    tracksNoMeta = []

    source = os.path.join(os.getcwd(), "testSongs") #os.getcwd()
    dest = os.path.join(source, "ohneMeta")
    destM = os.path.join(source, "mitMeta")

    print(source)
    print(dest)
    print(destM)
    print(os.path.exists(source))
    print(os.path.exists(dest))
    print(os.path.exists(destM))



    if not os.path.exists(dest):
    os.makedirs(dest)
    if not os.path.exists(destM):
    os.makedirs(destM)

    for root, dirs, files, in os.walk(source):
    for index, name in enumerate(files):
    if name.endswith((".mp3",".m4a",".flac",".alac")):
    tracks.append(name)
    try:

    temp_track = TinyTag.get(root + "\\" + name)

    #metadata available
    if temp_track.title and temp_track.artist:
    print(index, name, "->", temp_track.artist, "-", temp_track.title)
    tracksMeta.append(name + "->" + temp_track.title + "-" + temp_track.artist)

    #rename
    if os.access(source, os.W_OK):
    print(temp_track.title)
    print(temp_track.artist)

    newname = temp_track.title + " - " + temp_track.artist + ".mp3"
    if os.path.exists(source) and os.path.exists(destM):
    os.renames(os.path.join(source, name), os.path.join(destM, newname))

    #or not
    else:
    tracksNoMeta.append(name)
    #move
    if os.path.exists(source) and os.path.exists(dest):
    os.renames(os.path.join(source, name), os.path.join(dest, name))

    except TinyTagException:
    print("Error")

    Continue reading...

Compartilhe esta Página