Python:如何解决IndentationError

时间:2011-05-05 20:34:26

标签: python indentation

My IndentationError似乎无法解决。 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)

4 个答案:

答案 0 :(得分:13)

您有一个try区块(从第30行开始),没有except

答案 1 :(得分:5)

我没有看到第二个except的{​​{1}}块。这应该打破它,但我认为它不会给你try所以你可能会有更多问题。

答案 2 :(得分:5)

你有没有看过pep8(link)它会自动检查你的代码是否有错误。

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

答案 3 :(得分:2)

可能的原因是混合制表符和空格字符。你应该在任何地方使用所有这些。配置您的编辑器。建议的设置是4空格缩进。对于vim,这将是set ts=4 sw=4 expandtab

在您的问题中发布错误会降低投票率,而不是让人们抓住您的代码并自行运行......

正如@Mu Mind所说,你还有try块没有exceptfinally子句。既然你没有发布你的错误,我不能确定,但​​我敢打赌,如果你读它会说“第39行意外的缩进......”或类似内容。删除try或添加异常处理。

相关问题