def removeDeletedTracks():
  """This function will remove every song that is not currently accessible from your iTunes library"""
  #First, we create an instance of the iTunes Application
  itunes= win32com.client.Dispatch("iTunes.Application")
  
  #The ITTrackKindFile value will be use to check if a track is a physical file
  ITTrackKindFile=1
  
  #The tracks list object is obtained from the iTunes instance  with the following expression:
  #itunes.LibraryPlaylist.Tracks
  #this tracks list object has two fields/methods of interest:
  # The Count field contain the total number of tracks in your library
  # The Item(numTrack) method will return the track with number numTrack
  #     (Be aware that the numbering of the tracks start at 1, NOT at 0)
  mainLibrary = itunes.LibraryPlaylist
  tracks = mainLibrary.Tracks
  numTracks = tracks.Count
  deletedTracks=0
  
  #(optional)We can create a log file in which the names of the deleted songs are written
  #log=open("deletedTracks.txt","w")
  
  while numTracks  !=0:
    currTrack=tracks.Item(numTracks)
    if currTrack.Kind == ITTrackKindFile:
      if currTrack.Location == "":
        #Uncomment the lines below if you want to have the name of the deleted files saved in the log file
        #name_artist=currTrack.Artist
        #name_song=currTrack.Name
        #log.write("[%s] [%s]\n"%(name_artist.encode("utf-8"),name_song.encode("utf-8")))
        currTrack.Delete()
        deletedTracks+=1
    numTracks-=1
  
  
  if deletedTracks > 0:
    if deletedTracks == 1:
      print "Removed 1 dead track."
    else:
      print "Removed " + str(deletedTracks) + " dead tracks."
  else:
    print "No dead tracks were found."
This function will remove every song that is not currently accessible from your iTunes library
recipes/Python/498241_Scripting_iTunes_Windows/recipe-498241.py