else:语法不正确

tkd*_*_aj -1 python syntax if-statement syntax-error

我是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
Run Code Online (Sandbox Code Playgroud)

Eli*_*sky 5

你做:

if foo:
  bar
baz
else:
  bomb
Run Code Online (Sandbox Code Playgroud)

哪个错了.所有的线if及其相应的else必须缩进比更深ifelse,就像这样:

if foo:
  bar
  baz
else:
  bomb
Run Code Online (Sandbox Code Playgroud)