Python:如何解决缩进错误

发布于 2024-11-05 11:59:22 字数 1740 浏览 1 评论 0原文

我的 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你在看孤独的风景 2024-11-12 11:59:22

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

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

红颜悴 2024-11-12 11:59:22

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

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.

耀眼的星火 2024-11-12 11:59:22

您是否看过 pep8(链接),它会自动检查您的代码是否有错误。

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

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
单调的奢华 2024-11-12 11:59:22

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

在你的问题中发布你的错误将使这个问题不太可能被否决,而不是要求人们获取你的代码并自己运行它......

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文