Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40a9421

Browse files
Merge pull request #139 from lironmiz/unit9-Ex9.3.2
Create unit9_ex9.3.2.py
2 parents aa81349 + 4f2dd91 commit 40a9421

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

‎unit9_ex9.3.2.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# exercise 9.3.2 from unit 9
2+
'''
3+
def my_mp4_playlist(file_path, new_song):
4+
The function accepts as parameters:
5+
6+
Path to the file (string).
7+
Just like in the previous exercise, the file represents a playlist of songs and has a fixed format built from the name of the song, the name of the performer (singer/band) and the length of the song, separated by a semicolon (;) without spaces.
8+
9+
An example of an input file called songs.txt:
10+
Tudo Bom; Static and Ben El Tavori; 5:13;
11+
I Gotta Feeling; The Black Eyed Peas; 4:05;
12+
Instrumental; Unknown; 4:15;
13+
Paradise; Coldplay; 4:23;
14+
Where is the love?; The Black Eyed Peas; 4:13;
15+
A string representing the name of a new song.
16+
The operation of the function my_mp4_playlist:
17+
18+
The function writes to the file the string representing the name of a new song (new_song) instead of the name of the song that appears in the third line of the file (if the file contains less than three lines, write empty lines to the file so that the name of the song is written in the third line).
19+
The function prints the contents of the file after the change has been made.
20+
An example of running the my_mp4_playlist function on the songs.txt file
21+
>>> my_mp4_playlist(r"c:\my_files\songs.txt", "Python Love Story")
22+
Tudo Bom; Static and Ben El Tavori; 5:13;
23+
I Gotta Feeling; The Black Eyed Peas; 4:05;
24+
Python Love Story;Unknown;4:15;
25+
Paradise; Coldplay; 4:23;
26+
Where is the love?; The Black Eyed Peas; 4:13;
27+
The contents of the songs.txt file after
28+
29+
Tudo Bom;Static and Ben El Tavori;5:13;
30+
I Gotta Feeling;The Black Eyed Peas;4:05;
31+
Python Love Story;Unknown;4:15;
32+
Paradise;Coldplay;4:23;
33+
Where is the love?;The Black Eyed Peas;4:13;
34+
35+
'''
36+
def my_mp4_playlist(file_path, new_song):
37+
with open(file_path, 'r') as f:
38+
lines = f.readlines()
39+
40+
while len(lines) < 3:
41+
lines.append(";;\n")
42+
43+
song_parts = new_song.split(';')
44+
song_name = song_parts[0]
45+
artist = "Unknown"
46+
if len(song_parts) > 1:
47+
artist = song_parts[1]
48+
49+
lines[2] = f"{song_name};{artist};\n"
50+
51+
with open(file_path, 'w') as f:
52+
f.writelines(lines)
53+
54+
with open(file_path, 'r') as f:
55+
print(f.read())
56+
57+
def main():
58+
my_mp4_playlist(r"c:\my_files\songs.txt", "Python Love Story")
59+
60+
if __name__ == "__main__":
61+
main()
62+
63+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /