免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3705 | 回复: 4
打印 上一主题 下一主题

python脚本运行报异常错误,请帮忙看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-07-12 19:46 |只看该作者 |倒序浏览
各位:

我有段python脚本,运行时报错,信息如下:

2016-07-12 10:59:14,777 INFO  agent: worker uapgv4190.us.oracle.com-0 started
2016-07-12 10:59:16,186 ERROR uapgv4190.us.oracle.com-0: aborting process - Jython exception, <type 'exceptions.SyntaxError'>: ("no viable alternative at input '/'", ('./test_ldap.py', 70, 15, "      f = open(/tmp/ldaptest.log, 'a+')\n")) [initialising test script]
net.grinder.scriptengine.jython.JythonScriptExecutionException: <type 'exceptions.SyntaxError'>: ("no viable alternative at input '/'", ('./test_ldap.py', 70, 15, "      f = open(/tmp/ldaptest.log, 'a+')\n"))
2016-07-12 10:59:16,197 INFO  agent: finished


我的脚本就是不断运行ldap search命令,脚本如下:
  1. from net.grinder.script.Grinder import grinder
  2. from net.grinder.script import Test
  3. from datetime import datetime
  4. from datetime import timedelta
  5. import os
  6. import sys

  7. ########################################
  8. #
  9. # main body of test script starts here
  10. #
  11. ########################################

  12. # Get the propeties to access test configuration information
  13. properties = grinder.getProperties()

  14. # The description is a property (instead of a hardcoded string in this script)
  15. test = Test(1, properties.get("javatest.description"))

  16. # select the method for which to collect information
  17. #test.record(WriteMulitpleLittleFile.write)

  18. # initialize data for compressing
  19. #fileName = properties.get("javatest.fileToCompress")
  20. #grinder.logger.info("data file to compress is " + fileName)
  21. #JavaTest.initializeCompression(fileName)

  22. # If the run mode is runOnce, the TestRunner class will
  23. # run once.  Otherwise, if the run mode is continuous,
  24. # the TestRunner class will run the test for at least
  25. # the specified duration (but possibly longer)
  26. runMode = properties.get("javatest.runMode")
  27. #WriteMulitpleLittleFile.setParameters(dir, fileSize...)
  28. if runMode == "continuous":
  29.   # figure out how long to run the test
  30.   m = int(properties.getProperty("javatest.durationMinutes", "0"))
  31.   h = int(properties.getProperty("javatest.durationHours", "0"))
  32.   d = int(properties.getProperty("javatest.durationDays", "0"))
  33.   duration = timedelta(minutes=m,hours=h,days=d)
  34.   grinder.logger.info("run mode is continuous, duration is " + str(duration))
  35. elif runMode == "runOnce":
  36.   grinder.logger.info("run mode is run once")
  37.   duration = timedelta(minutes=0)
  38. else:
  39.   grinder.logger.info("run mode not set or not recongized, default to run once")
  40.   duration = timedelta(minutes=0)

  41. ########################################
  42. #
  43. # The TestRunner class is used by The Grinder to perform the test
  44. #
  45. ########################################

  46. grinder.statistics.registerSummaryExpression("connTimeout", "userLong4")

  47. class TestRunner:
  48.   def __call__(self):
  49.     endTime = datetime.now() + duration
  50.     notDone = True
  51.     while notDone:
  52.       notDone = datetime.now() < endTime

  53.       os.environ['ORACLE_HOME'] = "/u03/Middleware/Oracle_IDM"
  54.       from commands import getoutput
  55.       result = getoutput( '/u03/Middleware/Oracle_IDM/bin/ldapsearch -a always -D cn=orcladmin -w welcome1 -p 3060 -b "" -s sub "objectclass=*" orcldirectoryversion ou=Groups Privilege,   cn=OperationURLs,cn=DAS,cn=Products,cn=OracleContext,dc=cn,dc=oracle,dc=com')
  56. #print result
  57.       f = open(/tmp/ldaptest.log, 'a+')
  58.       f.write(result)
  59.       f.close()

  60.   def recordconnTimeout(connTimeout):
  61.      grinder.statistics.forCurrentTest.setValue("userLong4", connTimeout)
复制代码
以前脚本输出显示先屏幕上运行正常,添加了一段代码把查询信息输出在文件里就报错了,添加的代码如下:
  1. f = open(/tmp/ldaptest.log, 'a+')
  2.       f.write(result)
  3.       f.close()
复制代码
不知道哪里写错了? 请指点下 谢谢


论坛徽章:
0
2 [报告]
发表于 2016-07-12 20:04 |只看该作者
本帖最后由 yyl910606 于 2016-07-12 20:04 编辑
  1.       f = open(/tmp/ldaptest.log, 'a+')  #文件路径也是字符串 所以要用单引号或者双引号框起    f = open("/tmp/ldaptest.log", 'a+')
  2.       f.write(result)
  3.       f.close()
复制代码

论坛徽章:
0
3 [报告]
发表于 2016-07-12 20:27 |只看该作者
非常谢谢! 我试试

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
4 [报告]
发表于 2016-07-13 13:37 |只看该作者
      f = open(/tmp/ldaptest.log, 'a+')
      f.write(result)
      f.close()

这个是C思维,python下一般不需要close的,直接写

file("/tmp/ldaptest.log","a+").write(result)

论坛徽章:
0
5 [报告]
发表于 2016-07-13 16:00 |只看该作者
多谢指点。回复 4# bskay


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP