-
-
Save roman-on/5290af6cafebcdc7af0acb58fafec02f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The function accepts as parameters: | |
1. Path to File (String). | |
Just as in the previous exercise, the file represents a playlist of songs and has a fixed format built from the song name, performer name (singer / band), and song length, separated by semicolon (;) without spaces. | |
An example of an input file called songs. Text: | |
Todo Boom; Static and Ben El Tabori; 5:13; | |
I must feel; The black peas eyes; 4: 05; | |
Instrumental; Unknown; 4:15; | |
heaven; Coldplay; 4:23; | |
Where is the love ?; Black-eyed peas; 4:13; | |
2. A string representing the name of a new song. | |
My_mp4_playlist function: | |
1. The function writes to the file the string representing a new song name (new_song) instead of the key song name in the third line of the file (If the file contains less than your own lines, write to a blank line file so that the song name is written in the third line). | |
2. The function prints the contents of the file after the change has been made. | |
An example of running the my_mp4_playlist function on the songs.txt file | |
>>> my_mp4_playlist (r "c: \ my_files \ songs.txt", "Python Love Story") | |
Todo Boom; Static and Ben El Tabori; 5:13; | |
I must feel; The black peas eyes; 4: 05; | |
Python love story; Unknown; 4:15; | |
heaven; Coldplay; 4:23; | |
Where is the love ?; Black-eyed peas; 4:13; | |
Content of the songs.txt file after running the my_mp4_playlist function | |
Todo Boom; Static and Ben El Tabori; 5:13; | |
I must feel; The black peas eyes; 4: 05; | |
Python love story; Unknown; 4:15; | |
heaven; Coldplay; 4:23; | |
Where is the love ?; Black-eyed peas; 4:13; | |
""" | |
def my_mp4_playlist(file_path, new_song): | |
a = [] # New variable for list | |
c = file_path # Saving the file path into c variable in order to use it again and again later | |
with open (file_path, "r") as file_path: # Open and closing automatically the file | |
read_all_line = file_path.read() # Open read file | |
splited = read_all_line.split("\n") # Convert the lines into a list | |
for element in splited: # Extract the elements from your new list | |
a.append(element.split(";")) # Moving the extracted elements into a new variable "a" and spliting by ";" | |
b = read_all_line.replace(a[2][0], new_song) # Replacing the old word with a new words in specific area of the file and saving into the same file | |
with open (c, "w") as c: # Opens the file to write | |
c.write(b) # Writting into the file the new rewritten lines | |
my_mp4_playlist(r"C:\.....") | |
def main(): | |
<call the function> | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your code will replace the song name in the third line, but what if we have the same song name for the 3 and 4 line? both will be changed
for example:
Todo Boom; Static and Ben El Tabori; 5:13;
I must feel; The black peas eyes; 4: 05;
Instrumental; Unknown; 4:15;
Instrumental; Coldplay; 4:23;
Where is the love ?; Black-eyed peas; 4:13;
after the function:
Todo Boom; Static and Ben El Tabori; 5:13;
I must feel; The black peas eyes; 4: 05;
Python love story; Unknown; 4:15;
Python love story; Coldplay; 4:23;
Where is the love ?; Black-eyed peas; 4:13;