【问题标题】:Python: How to resolve IndentationErrorPython:如何解决 IndentationError
【发布时间】:2017-04-12 00:57:52
【问题描述】:

我的 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 个空格。
  • 如果正确答案为您解决了问题,您应该将其标记为“已接受”。点击答案旁边的复选标记。
  • 您可能指的是一个发布错误消息的人没有任何源代码(Q 肯定被删除了,我现在找不到了)。我认为您很好,但正如其他人所说,包含实际错误消息会使问题更好。

标签: python indentation


【解决方案1】:

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

【讨论】:

  • 就是这样!谢谢,我不知道每次你有一个 try 语句时你都必须有 and except 语句。
  • @wkoomson:你可以有一个没有 except 语句的 try/finally 块。但是如果你既没有 except 也没有 finally,那么 try 语句就没有用了。
  • @wkoomson: docs.python.org/reference/… 似乎很清楚。你用的是什么教程?
【解决方案2】:

我没有看到 except 阻止您的第二个 try。这应该会破坏它,但我认为它不会给您IndendationError,因此您可能会遇到更多问题。

【讨论】:

  • 确实给出了 IndentationError
  • 当我尝试它时它给了我一个SyntaxError,但我又试了一次,这次将try块放在if中,它产生了一个IndendationError,所以我想这取决于。
【解决方案3】:

您是否看过 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

【讨论】:

  • ...但是,这些样式警告实际上都与问题无关。
【解决方案4】:

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

在您的问题中发布您的错误会降低被否决的可能性,而不是要求人们获取您的代码并自己运行它......

正如@Mu Mind 所说,您还有一个没有exceptfinally 子句的try 块。由于您尚未发布错误,我无法确定,但我敢打赌,如果您阅读它,它会说“第 39 行意外取消缩进...”或类似内容。删除 try 或添加异常处理。

【讨论】:

  • 好的,谢谢。你知道我将如何配置 Geany 来做到这一点,因为那是我正在使用的 IDE。
  • @wkoomson:你看过 Geany 的帮助页面吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2012-11-18
  • 2014-02-20
  • 2011-09-10
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
相关资源
最近更新 更多