So I am making a script that reads a bunch of text files( One for each song) for lyrics. How it works is you input a lyric phrase and the script scans all the available files for those lyrics and tells you the name of the song. The problem is that the slashes don't work. I change the slashes between "/" and "\" but I run into errors.
when I use a forward slash, I see the following:
"OSError: [Errno 22] Invalid argument: ' C:/Users/[My Name]/Desktop/MusicLyricSearch/AllSongs/Old_Town_Road.txt'"
when I put back slashes I get the error:
"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape".
I have seen many other posts about how to do this like: Searching multiple text files for two strings? and (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
So, the first link is really what the code is, but I get the error
"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape"
The second link to fix this problem didn't really help either
This is my code:
from os import listdir
lyricSearch = input("Input the phrase from the song: ")
with open("C:/Users/[My Name]/Desktop/MusicLyricSearch/AllSongs/results.txt", "w") as f:
for filename in listdir("C:/Users/[My Name]/Desktop/MusicLyricSearch/AllSongs"):
with open(" C:/Users/Traner/Desktop/MusicLyricSearch/AllSongs/" + filename) as currentFile:
lyrics = currentFile.read()
if(lyricSearch in lyrics):
f.write("The song is", filename)
else:
f.write("Error: Could not find lyrics in any songs")
I am hoping to get the code to change mine to it to show me the file name of the song lyrics, instead I get the errors.
P.S. As you can probably tell because I basically copy the code, I am pretty new to coding in python.