python执行程序最后一行_执行python脚本时,最后一行(调用函数行)出现语法错误...

我试图通过一个使用pexpect的python脚本自动化一些任务。

我还使用了2.sh脚本。1调用这个python脚本,第二个调用二进制脚本。在

以下是我使用的文件:root@box: ~# cat while.sh

#!/bin/bash

while read line

do

./try $line

done < $1

root@box: ~# cat try

#!/usr/bin/python

import sys, pwd, os

sys.path.append('pexpect')

try:

import pexpect

except(ImportError):

print "\nYou need the pexpect module."

sys.exit(1)

#Change this if needed.

LOGIN_ERROR = 'not response! '

def connect(host):

print "Trying: ",host

child = pexpect.spawn ('./x '+host)

child.expect ('Enter string: ')

child.sendline ('blablastring')

i = child.expect([LOGIN_ERROR, pexpect.TIMEOUT], timeout=10)

if i == 1:

print "\n\t[!] OK:",host

child.sendline ('echo OK')

print "\nCommand executed successfully on:",host

print child.before

child.interact()

if i == 0:

print "Failed to exec cmd on: ",host

if len(sys.argv) != 1:

print "\nUsage : ./try "

print "Eg: ./try 1.3.3.7\n"

sys.exit(1)

target = sys.argv[1]

try:

connect(host)

root@box: ~# cat x

#!/bin/bash

./somescript -h $1 -d 22

root@box: ~#

root@box: ~# head -n 5 hosts

192.168.1.120

192.168.1.121

192.168.1.122

192.168.1.123

192.168.1.124

root@box: ~#

root@box: ~# sh while.sh hosts

File "./try", line 34

^

SyntaxError: invalid syntax

File "./try", line 34

^

SyntaxError: invalid syntax

File "./try", line 34

^

SyntaxError: invalid syntax

File "./try", line 34

^

SyntaxError: invalid syntax

File "./try", line 34

root@box: ~#

失败在哪里?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的内置模块ast来解析Python代码,找出其中直接或间接调用了numpy函数或类的部分。下面是一段示例代码: ```python import ast def find_numpy_calls(source_code): calls = [] # 解析代码 tree = ast.parse(source_code) # 遍历语法树 for node in ast.walk(tree): # 如果是函数调用节点 if isinstance(node, ast.Call): # 如果调用的是numpy函数或类 if hasattr(node.func, 'value') and node.func.value.id == 'numpy': calls.append(node) return calls # 示例代码 source_code = """ import numpy as np a = np.array([1, 2, 3]) b = np.zeros((3, 3)) c = np.random.rand(10) """ calls = find_numpy_calls(source_code) for call in calls: print(call) ``` 其中,`find_numpy_calls`函数接受一段Python代码作为输入,返回其中所有直接或间接调用了numpy函数或类的函数调用节点列表。在示例代码中,我们调用了`find_numpy_calls`函数,并输出了其中找到的所有调用节点。运结果如下: ``` Call( func=Attribute( value=Name(id='np', ctx=Load()), attr='array', ctx=Load(), ), args=[List(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load())], keywords=[], ) Call( func=Attribute( value=Attribute( value=Name(id='np', ctx=Load()), attr='zeros', ctx=Load(), ), attr='__call__', ctx=Load(), ), args=[Tuple(elts=[Num(n=3), Num(n=3)], ctx=Load())], keywords=[], ) Call( func=Attribute( value=Attribute( value=Name(id='np', ctx=Load()), attr='random', ctx=Load(), ), attr='rand', ctx=Load(), ), args=[Num(n=10)], keywords=[], ) ``` 可以看到,我们成功找到了代码中所有调用了numpy函数或类的部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值