在python中,在意料之外的令牌中出现语法错误。

时间:2022-04-18 22:44:34

I am a Python Novice so please help me out...

我是一个Python新手,所以请帮我……

#!/usr/bin/python -tt

import sys
import commands

def runCommands():
  f = open("a.txt", 'r')
  for line in f:  # goes through a text file line by line
    cmd = 'ls -l ' + line 
    print "printing cmd = " + cmd,
    (status, output) = commands.getstatusoutput(cmd)
  if status:    ## Error case, print the command's output to stderr and exit
      print "error"
      sys.stderr.write(output)
      sys.exit(1)
  print output
  f.close()

def main():
  runCommands()

# Standard boilerplate at end of file to call main() function.
if __name__ == '__main__':
  main()

I run it as follows:

我将其运行如下:

$python demo.py
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; } 2>&1'
error

Running less $(which python) says:

运行less $(python)说:

#!/bin/sh bin=$(cd $(/usr/bin/dirname "$0") && pwd) exec -a "$0" "$bin/python2.5" "$@"

If i remove for loop then it works fine

如果我移除for循环,它就会正常工作。

$cat a.txt
dummyFile


$ls -l dummyFile
-rw-r--r-- 1 blah blah ...................

$python demo.py
printing cmd = ls -l dummyFile
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; } 2>&1'
error

I am using 'ls' just for showing the problem. Actually i wanna use some internal shell scripts so i have to run this python script in this way only.

我用“ls”来表示问题。实际上,我想使用一些内部shell脚本,所以我只能以这种方式运行这个python脚本。

4 个解决方案

#1


4  

The problem is caused by this line:

问题是由这条线引起的:

    cmd = 'ls -l ' + line

it should be modified to:

应该修改为:

    cmd = 'ls -l ' + line.strip() 

When you read the line from your text file, you also read the trailing \n. You need to strip this so that it works. The getstatusoutput() doesn't like the trailing newline. See this interactive test (which is how I verified it):

当您从文本文件中读取该行时,您也会读取尾随\n。你需要把它拆开,这样它就能工作了。getstatusoutput()不喜欢尾随的新行。请看这个交互式测试(我是如何验证它的):

In [7]: s, o = commands.getstatusoutput('ls -l dummyFile')

In [8]: s, o = commands.getstatusoutput('ls -l dummyFile\n')
sh: Syntax error: ";" unexpected

#2


2  

This seems to be a problem with the "python" command, perhaps it's a shell wrapper script or something.

这似乎是“python”命令的一个问题,也许它是一个shell脚本或其他东西。

Run

运行

$ less $(which python)

UPDATE:

更新:

Try calling the Python executable directly, it seems to be at /usr/bin/python2.5:

尝试直接调用Python可执行文件,它似乎在/usr/bin/python2.5:

$ /usr/bin/python2.5 demo.py

#3


1  

The documentation for the commands module states that when you run getstatusoutput(cmd),

命令模块的文档说明,当您运行getstatusoutput(cmd)时,

cmd is actually run as { cmd ; } 2>&1

cmd实际上是作为{cmd;} 2 > & 1

This should explain where the ; } 2>&1 is coming from.

这应该解释为什么;>&1来自。

My first guess is that the problem is being caused by not stripping the newlines off the end of each line you read from the file, and so the command you're actually running is something like

我的第一个猜想是,问题的原因是,您从文件中读取的每一行的末尾没有去掉换行符,所以您实际运行的命令是类似的。

{ ls -l somedir
; } 2>&1

However, I don't know shell programming very well so I don't know how sh will cope with the contents of the { ... } split over two lines, nor why it reports the problem on line 1 when there are now two lines.

但是,我不太了解shell编程,所以我不知道sh将如何处理{……分两行,也不知道为什么在第一行有两条线时就会出现问题。

A second guess is that there's a blank line in your file, in which case sh may be complaining because it's looking for an argument for ls and it found ; } 2>&1 instead.

第二种猜测是,在你的文件中有一个空行,在这种情况下,sh可能会抱怨,因为它在寻找ls的参数,它找到了;} 2 > & 1代替。

A third guess is that one of the files contains a }, or maybe a ; followed by a }.

第三个猜测是其中一个文件包含一个},或者可能是一个;其次是}。

Ultimately, I can't say for sure what the problem is without seeing the contents of your file a.txt.

最后,我不能确定问题是什么,没有看到文件的内容。

Incidentally, I hope this file doesn't contain a line / && sudo rm -rf /, as that might cause you one or two problems.

顺便说一下,我希望这个文件不包含一个行/ && sudo rm -rf /,因为那样可能会导致一个或两个问题。

#4


1  

Got this answer from somewhere else:

从其他地方得到了这个答案:

When you iterate through a file as an iterator, newlines are NOT stripped. The following is ACTUALLY what your script is executing. The fact that you have a trailing comma on your print statement (and a newline on your output) is the giveaway.

当您作为迭代器遍历一个文件时,不会删除新行。下面是您的脚本正在执行的内容。在打印语句上有一个尾随逗号(在输出上有一条换行符),这是免费的。

ls -l dummyFile \n

Which commands interprets as

这命令解释

{ ls -l dummyFile
; } 2>&1

Call line.rstrip() (or just strip) to fix it.

调用line.rstrip()(或只是条)来修复它。

cmd = 'ls -l ' + line.strip()

#1


4  

The problem is caused by this line:

问题是由这条线引起的:

    cmd = 'ls -l ' + line

it should be modified to:

应该修改为:

    cmd = 'ls -l ' + line.strip() 

When you read the line from your text file, you also read the trailing \n. You need to strip this so that it works. The getstatusoutput() doesn't like the trailing newline. See this interactive test (which is how I verified it):

当您从文本文件中读取该行时,您也会读取尾随\n。你需要把它拆开,这样它就能工作了。getstatusoutput()不喜欢尾随的新行。请看这个交互式测试(我是如何验证它的):

In [7]: s, o = commands.getstatusoutput('ls -l dummyFile')

In [8]: s, o = commands.getstatusoutput('ls -l dummyFile\n')
sh: Syntax error: ";" unexpected

#2


2  

This seems to be a problem with the "python" command, perhaps it's a shell wrapper script or something.

这似乎是“python”命令的一个问题,也许它是一个shell脚本或其他东西。

Run

运行

$ less $(which python)

UPDATE:

更新:

Try calling the Python executable directly, it seems to be at /usr/bin/python2.5:

尝试直接调用Python可执行文件,它似乎在/usr/bin/python2.5:

$ /usr/bin/python2.5 demo.py

#3


1  

The documentation for the commands module states that when you run getstatusoutput(cmd),

命令模块的文档说明,当您运行getstatusoutput(cmd)时,

cmd is actually run as { cmd ; } 2>&1

cmd实际上是作为{cmd;} 2 > & 1

This should explain where the ; } 2>&1 is coming from.

这应该解释为什么;>&1来自。

My first guess is that the problem is being caused by not stripping the newlines off the end of each line you read from the file, and so the command you're actually running is something like

我的第一个猜想是,问题的原因是,您从文件中读取的每一行的末尾没有去掉换行符,所以您实际运行的命令是类似的。

{ ls -l somedir
; } 2>&1

However, I don't know shell programming very well so I don't know how sh will cope with the contents of the { ... } split over two lines, nor why it reports the problem on line 1 when there are now two lines.

但是,我不太了解shell编程,所以我不知道sh将如何处理{……分两行,也不知道为什么在第一行有两条线时就会出现问题。

A second guess is that there's a blank line in your file, in which case sh may be complaining because it's looking for an argument for ls and it found ; } 2>&1 instead.

第二种猜测是,在你的文件中有一个空行,在这种情况下,sh可能会抱怨,因为它在寻找ls的参数,它找到了;} 2 > & 1代替。

A third guess is that one of the files contains a }, or maybe a ; followed by a }.

第三个猜测是其中一个文件包含一个},或者可能是一个;其次是}。

Ultimately, I can't say for sure what the problem is without seeing the contents of your file a.txt.

最后,我不能确定问题是什么,没有看到文件的内容。

Incidentally, I hope this file doesn't contain a line / && sudo rm -rf /, as that might cause you one or two problems.

顺便说一下,我希望这个文件不包含一个行/ && sudo rm -rf /,因为那样可能会导致一个或两个问题。

#4


1  

Got this answer from somewhere else:

从其他地方得到了这个答案:

When you iterate through a file as an iterator, newlines are NOT stripped. The following is ACTUALLY what your script is executing. The fact that you have a trailing comma on your print statement (and a newline on your output) is the giveaway.

当您作为迭代器遍历一个文件时,不会删除新行。下面是您的脚本正在执行的内容。在打印语句上有一个尾随逗号(在输出上有一条换行符),这是免费的。

ls -l dummyFile \n

Which commands interprets as

这命令解释

{ ls -l dummyFile
; } 2>&1

Call line.rstrip() (or just strip) to fix it.

调用line.rstrip()(或只是条)来修复它。

cmd = 'ls -l ' + line.strip()