-1

我对 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
4

2 回答 2

5

You do:

if foo:
  bar
baz
else:
  bomb

Which is wrong. All the lines between the if and its corresponding else must be indented deeper than the if and else, like this:

if foo:
  bar
  baz
else:
  bomb
于 2012-05-24T05:59:39.440 回答
3

The else is at the same indentation as the previous line, but the statement on the previous line doesn't have an else clause. Fix your indentation.

于 2012-05-24T05:59:19.923 回答