0

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.

razdi
  • 1,388
  • 15
  • 21
Sam
  • 3
  • 2
  • Use forward slashes but omit the space before the `C:`. If not done already you should work through the [Python tutorial](https://docs.python.org/3/tutorial/) – Michael Butscher May 06 '19 at 02:43
  • Try using backslashes but add `r` before the string. The `\U` when you type user is processed as the beginning of an eight character unicode escape code. Something like this: open(r'C:\Users\[My Name]\Desktop\MusicLyricSearch\AllSongs\results.txt', "w") – razdi May 06 '19 at 02:58

1 Answers1

0
from os import listdir

lyricSearch = input("Input the phrase from the song: ")

with open(r"C:\Users\[My Name]\Desktop\MusicLyricSearch\AllSongs\results.txt", "w") as f:
    for filename in listdir(r"C:\Users\[My Name]\Desktop\MusicLyricSearch\AllSongs"):
        with open(r"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")

The error comes from \U that occurs when writing \User. This acts as the beginning of an eight character unicode escape but since you continue with the file path, python is not able to interpret that escape code and spits out an error. The r at the beginning of the string forces it to be treated as a raw string and hence the unicode escape is not considered.

razdi
  • 1,388
  • 15
  • 21