Skip to content

Instantly share code, notes, and snippets.

@CRImier
Last active October 26, 2021 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CRImier/9777606 to your computer and use it in GitHub Desktop.
Save CRImier/9777606 to your computer and use it in GitHub Desktop.
This script is for exporting your playlist items to a single folder. Save, change two variables to suit your needs and run =) One use case is: you have a music collection and a playlist containing the best songs of this collection, and you want to load only the best songs to your MP3 player with limited space. This also doesn't overwrite files, …
import os
import shutil
directory_name = "playlist/"
playlist_name = "playlist.m3u"
f = open(playlist_name, "r")
files = []
for line in f:
line = line.strip().strip("\n").strip("\r")
if line and not line.startswith("#"):
files.append(line)
dir_contents = os.listdir(directory_name)
for file in files:
try:
filename = os.path.basename(file)
if filename not in dir_contents:
shutil.copy(file, directory_name)
print filename
else:
print ".", #File already there
except Exception as e:
print str(e)
@mibodhi
Copy link

mibodhi commented Oct 25, 2021

Nice script :)

Suggestion: Even better if you can assign a numerical prefix to the mp3 file names (e.g 001, 002,...) to preserve the oder of the song in the playlist.

-bodhi

@CRImier
Copy link
Author

CRImier commented Oct 25, 2021

Hey @mibodhi ! This wasn't needed for me, but you can certainly do this if you change line 14 to for i, file in enumerate(files): and line 16 to filename = str(i).zfill(3)+"_"+os.path.basename(file). Hope this helps! -Arsenijs

@mibodhi
Copy link

mibodhi commented Oct 26, 2021

Cool! I'll give it a try.
-bodhi

@mibodhi
Copy link

mibodhi commented Oct 26, 2021

I think line 18 still uses the original name. So we also need to somehow to rename the current file to filename:
shutil.copy(file, directory_name)

-bodhi

@CRImier
Copy link
Author

CRImier commented Oct 26, 2021

Indeed! Add file = os.path.join(os.path.dirname(file), filename) after line 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment