else:语法不正确

时间:2012-05-24 05:50:48

标签: python syntax syntax-error if-statement

我对python有点新,我正在尝试编写此脚本来取消超过1 mb的打印作业..(检查大小的行设置为1 mb以确保它正常工作)。由于某种原因,我的最后一个else语句一直说它语法无效。我查看是否所有括号都关闭了,我找不到一对不匹配的。有人可以告诉我为什么它说它无效?你也可以看看我的第24行(fullname = ... grep ...),以确保其语法正确吗?

#! /usr/bin/python

import os

infile = open ('test.pl', 'r')

outfile = open('print.reportpython', 'w+')

newfile = infile.readlines()

newfile.pop(0)

count = 0

firstline = newfile[0]

splitline = firstline.split()

currentuser = splitline[1]

currentuser = str(currentuser)

for line in newfile:

  newline = line.split()

  names = newline[1]

  size = int(newline[2])

  names = str(names)

  print names

  if names is currentuser:

    if size >= 1:

      os.popen ("cancel lab01-10292")

  fullname = os.popen("cat /etc/passwd |grep " + newline[1] + "cut -d':' -f5")

  count += 1

  print count

  else:

    print outfile.write ("(" + currentuser + ")")

    print outfile.write (" ")

    count = 0

    currentuser = names

2 个答案:

答案 0 :(得分:5)

你这样做:

if foo:
  bar
baz
else:
  bomb

哪个错了。 if及其对应的else之间的所有行必须缩进到比ifelse更深的位置,如下所示:

if foo:
  bar
  baz
else:
  bomb

答案 1 :(得分:3)

else与前一行的缩进相同,但前一行的语句没有else子句。修复你的缩进。

相关问题