So I want to rename MP3 files using their own metadata and sort them out (with/without metadata) and get sketchy results and a weird FileNotFoundError
while doing 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 sub directory!?
Error:
FileNotFoundError: [WinError 2]
The code:
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")
So I want to rename MP3 files using their own metadata and sort them out (with/without metadata) and get sketchy results and a weird FileNotFoundError
while doing 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 sub directory!?
Error:
FileNotFoundError: [WinError 2]
The code:
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")
Share
Improve this question
edited Mar 14 at 7:25
AmigoJack
6,1852 gold badges19 silver badges34 bronze badges
asked Mar 13 at 23:58
Mertus MentolesMertus Mentoles
1
1
- That seems like a classic case of modifying a directory while iterating through it. Moving files into a subdirectory of source can cause them to be detected again during traversal, leading to unpredictable behavior. To avoid this, either use a destination outside source or first collect the file list before renaming. – Konstantin Commented Mar 14 at 11:11
1 Answer
Reset to default 1Based on the information provided, and without the actual files and data, it seems likely that the file name you are attempting to create has \
or /
in it, and then it tries to write that file and instead sticks it in the directory with part of the name prior to the slash and a file name with part of the name after the slash.
Example:
If the song title is "21C/Delta" by "Jack Harlow", then the path created will be something like "C:/users/MertusMentoles/Music/testSongs/21C/Delta - Jack Harlow.mp3" which creates that extra directory with part of the name as "21C" and the rest as the file name.
If you check the song names and artist names, do any have any slashes? It would be best to sanitize those inputs prior to just handing it to the os.renames