Python Programming Method to modify the name of an MP3 file,
This article describes how to modify the name of an MP3 file through Python programming. We will share this with you for your reference. The details are as follows:
I recently started to learn Python. By the way, I wrote a small function that is used to modify the names of mp3 songs downloaded from the Internet. How can I use this function?
ModifyMp3FileInfo (r'e:/music/12345.mp3 ')
Python code:
#! Modifying the downloaded Mp3 file name is called the correct Mp3 file def ModifyMp3FileInfo (filename): mp3Id3V1 = {"tag": {"valuepos" :( 0, 3), "value ":""}, "SongName": {"valuepos" :( 3,33), "value": ""}, "SongPeople": {"valuepos" :( 33,63), "value ":""}, "Zj": {"valuepos" :( 63,93), "value": ""}, "Year": {"valuepos" :( 93,97), "value ":""}, "Bak": {"valuepos" :( 97,125), "value": ""} try: f = open (filename, 'rb') f. seek (-128,2) sdata = f. read (3) if sdata = 'tag': f. seek (-, 2) sdata = f. read (128) for tag, subitem in mp3Id3V1. items (): subitem ["value"] = sdata [subitem ["valuepos"] [0]: subitem ["valuepos"] [1]. replace ('/00 ',''). strip () print '% s =' % tag, '% s' % subitem ["value"],'/N' f. close () import OS if mp3Id3V1 ["SongName"] ["value"]! = '': Test = [OS. path. dirname (filename), '//'] test. append (mp3Id3V1 ["SongName"] ["value"]) test.append('hangzhou') newfilename = ''. join (test) print newfilename if OS. path. exists (newfilename): test = ['filename', newfilename, 'has existed'] print ''. join (test) else: try: OS. rename (filename, newfilename) Doesn't WindowsError, e: if e. winerror: print 'modify filename failed, maybe the file is inuse 'else: print 'unknown error' else: print 'Is not a MP3 file' handle T IOError: print 'open file failed'