Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a program that will go to a website and download all of the songs they have posted. Right now I'm having trouble creating new file names for each of the songs I download. I initially get all of the file names and the locations of the songs (html). However, when I try to create new files for the songs to be put in, I get an error saying:

IOError: [Errno 22] invalid mode ('w') or filename

I have tried using different modes like "w+", "a", and, "a+" to see if these would solve the issue but so far I keep getting the error message. I have also tried "% name"-ing the string but that has not worked either. My code is following, any help would be appreciated.

import urllib
import urllib2

def earmilk():
    SongList = []
    SongStrings = []
    SongNames = []
    earmilk = urllib.urlopen("http://www.earmilk.com/category/pop")
    reader = earmilk.read()
    #gets the position of the playlist
    PlaylistPos = reader.find("var newPlaylistTracks = ")
    #finds the number of songs in the playlist
    NumberSongs = reader[reader.find("var newPlaylistIds = " ): PlaylistPos].count(",") + 1
    initPos = PlaylistPos

    #goes though the playlist and records the html address and name of the song

    for song in range(0, NumberSongs):
        songPos = reader[initPos:].find("http:") + initPos
        namePos = reader[songPos:].find("name") + songPos
        namePos += reader[namePos:].find(">")
        nameEndPos = reader[namePos:].find("<") + namePos
        SongStrings.append(reader[songPos: reader[songPos:].find('"') + songPos])
        SongNames.append(reader[namePos + 1: nameEndPos])
        #initPos += len(SongStrings[song])
        initPos = nameEndPos

    for correction in range(0, NumberSongs):
        SongStrings[correction] = SongStrings[correction].replace('\\/', "/")

    #downloading songs

    #for download in range(0, NumberSongs):
    #print reader.find("So F*")
    #x= SongNames[0]
    songDL = open(SongNames[0].formant(name), "w+")
    songDL.write(urllib.urlretrieve(SongStrings[0], SongNames[0] + ".mp3"))
    songDL.close()


    print SongStrings
    for name in range(0, NumberSongs):
        print SongNames[name] + "\n"
    earmilk.close()
share|improve this question
 
Try print SongNames[0].formant(name), before open –  thefourtheye Dec 14 at 7:34
add comment

1 Answer

You need to use filename = '%s' % (SongNames[0],) to construct the name but you also need to make sure that your file name is a valid one - I don't know of any songs called *.* but I wouldn't like to chance it so something like:

filename = ''.join([a.isalnum() and a or '_' for a in SongNames[0]])
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.