-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

あなたがやる:

if foo:
  bar
baz
else:
  bomb

これは間違っています。ifとそれに対応する間のすべての行は、次のようにandelseよりも深くインデントする必要があります。ifelse

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

前のelse行と同じインデントにありますが、前の行のステートメントにはelse節がありません。インデントを修正します。

于 2012-05-24T05:59:19.923 に答える