Copy music from iPod to computer

hondros 0 Tallied Votes 503 Views Share

This simple script will copy all music files that were not purchased from the iTunes store to a directory of your choosing. It requires the ID3 module (Created by some person, from somewhere, I forget where, but I DID NOT make this module). The syntax is as follows:

ipoddir = 'E:'
newdir = 'C:\\Documents and Settings\\#\\My Documents\\ipodtest'
a = iPod(ipoddir, newdir debug='on')

That's it, it'll run just like that, and show you which file it's working on, and where it's putting it. It'll organize it by Artist, then Album. As of now, I have not included options for organizing, or for going through steps yet. But it will copy the files, so long as the iPod is not a iPod touch or iPhone
ID3 Module: http://id3-py.sourceforge.net/

'''
iPod Music Copy Module
Copies the music from your iPod to a folder on your computer
'''

from os import rename,  \
               chdir,   \
               getcwd,  \
               listdir, \
               mkdir,   \
               path
from shutil import copy2
from ID3 import ID3

class iPod():

    def __init__(self, iPod, direct, new='no', debug='off', organize='yes'):
        '''
        iPod is the root directory of the iPod
        direct is the directory to copy to
        define new to 'yes' to take you through steps
        '''
        if new != 'yes':
            if len(iPod) == 1:
                iPod = iPod + ":"
            if len(iPod) > 2:
                iPod = iPod[0:2]
            self.iPoddir = iPod+"\\iPod_Control\\Music"
            if (path.exists(self.iPoddir) == False) or (path.exists(direct) == False):
                raw_input("Please press 'Enter' to quit the application\nInvalid iPod or directory")
                exit()
            self.newdirect = direct
            self.organize = organize
            self.debug = 'on'
            self.runipodchange2()
        if new == 'yes':
            print("In order to copy the music from your iPod to your computer, there are a few steps you need to do.")
            print("First off, you need to make your iPod seeable from My Computer")
            print("Also, you'll need to disable auto-sync in order to keep your iPod music in-tact")
            print("If it's not off, simply hold SHIFT and CTRL while plugging in the iPod")
            print("In order to view the iPod in My Computer, ")

    def runipodchange2(self):
        # Change the directory to the directory of the ipod
        chdir(self.iPoddir)
        # List the directory
        folders = listdir('')
        if self.debug == 'on': print(self.iPoddir, folders)
        # Make a list of all songs to keep track of len()
        songstobecopied = []
        for folder in folders:
            for files in listdir(folder):
                songstobecopied.append(files)
        if self.debug == 'on': print(songstobecopied)
        songstobecopied = len(songstobecopied)
        # Make a counter to keep track of which file we're using
        cursong = 0
        # Start the loop to copy folders
        for folder in folders:
            for files in listdir(folder):
                cursong += 1
                if self.debug == 'on': print("Copying file: %s; %d of %d" %(files, cursong, songstobecopied))
                oldfile = self.iPoddir+"\\"+folder+"\\"+files
                # Open the song to check ID3 tags
                song = ID3(oldfile)
                # Strip the filepath of any '/' and ':'
                newdir = self.newdirect + "\\"
                newdir += '_'.join(song.artist.split('/')) + "\\"
                newdir += '_'.join(song.album.split('/')) + "\\"
                newdir = ''.join(newdir.split(':'))
                newdir = newdir[0]+":"+newdir[1:]
                if self.debug == 'on': print(newdir)
                newdirartist = ''
                for x in newdir.split('\\')[:-3]:
                    newdirartist += x + "\\"
                newdirartist += newdir.split('\\')[-3]
                #print(newdirartist)
                try:
                    mkdir(newdirartist)
                except:
                    pass
                try:
                    mkdir(newdir)
                except:
                    pass
                if path.exists(newdir+"\\"+files) != True:
                    songname = newdir+"\\"+files
                    copy2(oldfile, songname)
                elif path.exists(newdir+"\\"+files) == True:
                    asdf = 0
                    while path.exists(newdir+"\\"+str(asdf)+" "+files) == True:
                        asdf += 1
                    songname = newdir+"\\"+str(asdf)+" "+files
                    copy2(oldfile, songname)
                # Now is time to rename the file
                if songname.split('\\')[-1] != song.title+".mp3":
                    newname = '\\'.join(songname.split('\\')[:-1])+song.title+".mp3"
                    if path.exists(newname) != True:
                        rename(songname, '\\'.join(songname.split('\\')[:-1])+song.title+".mp3")
                    elif path.exists(newname) == True:
                        filecount = 0
                        while path.exists(newname[:-4]+str(filecount)+".mp3"):
                            filecount += 1
                        newname = newname[:-4]+str(filecount)+".mp3"
                        rename(songname, newname)
                song.file.close()

Dani AI

Generated

— nice, practical starter script. It will still work on many classic iPods, but it’s written in older Python style and relies on brittle string/path handling and an out-of-date ID3 library. That makes it easy to hit problems today: missing or different tag formats, non-MP3 files, Windows path edge cases, Unicode in artist/album names, and modern devices (iPod touch / iPhone) that don’t expose their music the same way.

Practical, low-risk updates to make it robust today:

  • Move to Python 3 (use print() and input()), and use pathlib or os.path.join instead of building paths with string slicing and manual backslashes.
  • Replace the legacy ID3 parser with a maintained tag library (one that supports MP3 and AAC/m4a).
  • Sanitize filenames (remove <>:"/\|?*), preserve the original extension, and create target directories with mkdir(parents=True, exist_ok=True) (or the try/except fallback if you must support Python 2).
  • Avoid bare except:; catch specific exceptions (e.g., OSError, IOError) and log failures.
  • For duplicates, prefer checking file size or computing a small hash (MD5) to detect identical content rather than blindly appending numbers to names.

Example of a small, safe-folder helper (Python 3):

import re
from pathlib import Path

def safe_name(s):
    return re.sub(r'[<>:"/\\|?*]', '_', s).strip()

target = Path('C:/Music') / safe_name(artist) / safe_name(album)
target.mkdir(parents=True, exist_ok=True)

Troubleshooting notes: if the iPod music folder isn’t visible, confirm the device is mounted and hidden/system files are shown; permission errors usually mean write access is needed; blank or missing tags are best handled with a fallback into an “Unknown Artist/Album” bucket; DRM-protected tracks or modern iOS devices may require different tools or APIs. These small changes keep the original approach but make it safe and maintainable for current systems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.