“缩进错误:unindent与任何外部缩进级别都不匹配”

时间:2013-12-23 20:28:06

标签: python indentation

我有错误:“缩进错误:unindent与我在第10行的文件的这一部分中抛出的任何外部缩进级别都不匹配。”我尝试重新缩进所有内容但我找不到为什么它在这里抛出这个错误的原因是:

import os
import time


if not os.path.exists("Desktop/ServerNotify/ServerNotifier.txt"):
    os.makedirs("Desktop/ServerNotify/ServerNotifier.txt")

def WriteAddressToFile():
    theFile = open("ServerList.txt")

  for line in theFile:
    if line == server:
      theFile.close()
    else:
      theFile.close()

      theFile = open("ServerList.txt", "a")
      theFile.write(server + "\n")
      theFile.close()

1 个答案:

答案 0 :(得分:2)

您的for-block需要缩进两个空格:

def WriteAddressToFile():
    theFile = open("ServerList.txt")

    for line in theFile:
      if line == server:
        theFile.close()
      else:
        theFile.close()

        theFile = open("ServerList.txt", "a")
        theFile.write(server + "\n")
        theFile.close()

注意它现在如何与这一行对齐:

theFile = open("ServerList.txt")

另外,只是有点唠叨,缩进的Python标准是4个空格。 :)