3

My IndentationError just seems so irresolvable. http://pastebin.com/AFdnYcRc.

#!/usr/bin/env python
import os
import glob
import shutil
import mutagen
from sys import exit

musicdir = raw_input("What directory are the music files located in? : ")
musfile = glob.glob(musicdir + '/' + "*.mp3")
musfile1 = glob.glob(musicdir + '/' + "*.flac")
musfile.extend(musfile1)
newmusicdir = raw_input("What directory should the music files be organized into? : ")


done = False

while not done:
    for m in musfile:
        if musfile:
            try:
                musta = mutagen.File(m, easy=True)
                mar = str(musta['artist'][0])
                mal = str(musta['album'][0])
                mti = str(musta['title'][0])
                mtr = str(musta['tracknumber'][0])
                os.makedirs(newmusicdir + '/' + mar + '/' + mal + '/')
            except OSError:
                pass
            finally:
                try:
                    if m.endswith('.mp3'):
                        os.rename(m,mtr + ' - ' + mar + ' - ' + mti + '.mp3')
                        m =mtr + ' - ' + mar + ' - ' + mti + '.mp3'
                        shutil.move(m,newmusicdir + '/' + mar + '/' + mal + '/')
                    elif m.endswith('.flac'):
                        os.rename(m,mtr + ' - ' + mar + ' - ' + mti + '.flac')
                        m = mtr + ' - ' + mar + ' - ' + mti + '.flac'
                        shutil.move(m,newmusicdir + '/' + mar + '/' + mal + '/')
        elif not musfile:
                print "Looks like we're done here. Please press <enter> to exit"
                raw_input()
                sys.exit(0)
Cœur
  • 37,241
  • 25
  • 195
  • 267
wkoomson
  • 3,077
  • 2
  • 15
  • 10
  • 1
    Post the exact error - it is likely to contain a line number! – Kylotan May 05 '11 at 20:37
  • Also post the code near the error. It's likely to be indented improperly. Finally, please replace all tab characters with 4 spaces before you post the code here. – S.Lott May 05 '11 at 20:41
  • 2
    You should mark the correct answers as "accepted" if they solve the problem for you. Click the checkmark next to the answer. – Daenyth May 05 '11 at 20:43
  • You might be referring to a guy who posted an error message *without any source code* (Q must have been deleted, I can't find it now). I think you're fine, but as others have said it would have made the question better to include the actual error message. – Mu Mind May 05 '11 at 20:44

4 Answers4

13

You have a try block (starting on line 30) with no except

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
  • THAT WAS IT! Thanks, I didn't know that you had to have and except statement everytime you had a try statement. – wkoomson May 05 '11 at 20:41
  • 2
    @wkoomson: You can have a try/finally block with no except statement. But if you've got neither except nor finally, the try statement is useless. – Thomas K May 05 '11 at 20:43
  • @wkoomson: http://docs.python.org/reference/compound_stmts.html#grammar-token-try_stmt seems pretty clear. What tutorial are you using? – S.Lott May 05 '11 at 20:48
5

I don't see an except block for your second try. That should break it, but I don't think it gives you a IndendationError so you might have more problems.

Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
5

Have you ever looked at pep8 ( link ) it automatically checks your code for errors.

test.py:12:80: E501 line too long (86 characters)
test.py:18:1: W191 indentation contains tabs
test.py:32:18: E231 missing whitespace after ','
test.py:33:10: E225 missing whitespace around operator
test.py:42:16: W292 no newline at end of file
Mu Mind
  • 10,935
  • 4
  • 38
  • 69
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
2

The likely cause is mixed tab characters and space characters. You should be using all of one or the other everywhere. Configure your editor to do so. The recommended setting is 4-space indents. For vim, this would be set ts=4 sw=4 expandtab.

Posting your error in your question would make this less likely to be downvoted, instead of asking people to grab your code and run it themselves...

As @Mu Mind said, you also have a try block with no except or finally clause. Since you haven't posted your error, I can't be sure, but I bet if you read it it will say something line "Unexpected de-indent at line 39...", or similar. Either remove that try or add exception handling.

Daenyth
  • 35,856
  • 13
  • 85
  • 124