I have a .txt file with a fixed format:
Tudo Bom;Static and Ben El Tavori;5:13;
I Gotta Feeling;The Black Eyed Peas;4:05;
Instrumental;Unknown;4:15;
Paradise;Coldplay;4:23;
Where is the love?;The Black Eyed Peas;4:13;
I'm trying to replace the name of the song in the third line with a new name.
I've written a function called my_mp4_playlist
which takes two parameters:
The first one is the file path (string) and the second is the new song name (string).
I'm trying to first get the file content with the read()
function and then loop the file lines till the third one and split the line with ";"
.
My problem is that when I use the read()
function, there's nothing to split, and when I don't read, the split works just fine.
The code (not finished yet) is looking like this:
def my_mp4_playlist(file_path, new_song):
with open(file_path, "r+") as f: # we using r+ for reading and writing and not overriding ALL text in the file
file_source = f.read()
third_line_list = []
for i, line in enumerate(f, 0):
if i == 2:
print(line.split(";"))
third_line_list = line.split(";")
break
The output of the print statement is just nothing.
But if I'm commenting the f.read()
line the output is:
['Instrumental', 'Unknown', '4:15', '\n']
Why is this happening? I'd like to know in a more general way and not a specific solution to my problem.