2011-05-05 62 views
3

My IndentationError看起来如此无法解决。 http://pastebin.com/AFdnYcRcPython:如何解决IndentationError

#!/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) 
+1

发布确切的错误 - 它可能包含行号! – Kylotan 2011-05-05 20:37:13

+0

也发布错误附近的代码。它可能会被不适当地缩进。最后,请在此处发布代码之前,用4个空格替换所有制表符。 – 2011-05-05 20:41:17

+2

如果他们为您解决问题,您应该将正确答案标记为“已接受”。点击答案旁边的复选标记。 – Daenyth 2011-05-05 20:43:47

回答

5

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

+0

它*给* IndentationError – codeape 2011-05-05 20:42:05

+2

当我尝试它时,它给了我一个'SyntaxError',但我又给了它一次,这次把'try'块放在'if'中,它产生了一个'IndendationError',所以我想这取决于。 – 2011-05-05 21:21:50

2

可能的原因是混合了制表符和空格字符。你应该使用所有的一个或其他地方。配置您的编辑器来执行此操作。推荐的设置是4格缩进。对于vim,这将是set ts=4 sw=4 expandtab

张贴在你的问题你的错误将使这不太可能downvoted而不是问人抢你的代码并运行它自己,...

正如@Mu心说,你也有一个try块没有exceptfinally条款。既然你没有发布你的错误,我不能确定,但​​我敢打赌,如果你阅读它,它会说“行39意外缩小...”或类似的内容。删除try或添加异常处理。

+0

好的,谢谢。你有一个想法,我将如何配置Geany来做到这一点,因为这是我使用的IDE。 – wkoomson 2011-05-05 20:39:16

+1

@wkoomson:http://stackoverflow.com/questions/5287849/how-to-stay-tab-free-in-geany-on-ubuntu/5287910#5287910 – Daenyth 2011-05-05 20:40:50

+0

@wkoomson:你看过Geany的帮助页吗? ? – 2011-05-05 20:41:41

13

你有一个try块(开始于第30行)没有except

+0

这是它!谢谢,我不知道你每次尝试发表声明都必须要有除了声明。 – wkoomson 2011-05-05 20:41:36

+2

@wkoomson:你可以有一个try/finally块,没有except语句。但是如果你除了最后都没有,那么try语句就没用了。 – 2011-05-05 20:43:25

+0

@wkoomson:http://docs.python.org/reference/compound_stmts.html#grammar-token-try_stmt似乎很清楚。你正在使用什么教程? – 2011-05-05 20:48:31

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 
+0

...但是,这些样式警告都没有涉及到这个问题。 – 2012-02-17 05:52:58