【问题标题】:python os.system error: "global name 'output' is not defined"python os.system 错误:“未定义全局名称‘输出’”
【发布时间】:2012-08-10 18:48:01
【问题描述】:

这里是新手。任何帮助将不胜感激..

我正在编写一个运行 tcp pcap 诊断工具的 cgi 脚本。如果我在 bash 中运行命令,它看起来像:

/home/fsoft/cap/capnostic -r 38350 /home/fsoft/brad.pcap > 38350

所以我想在 python 中做到这一点:

output = os.system('/home/fsoft/cap/capnostic -r' + port + directory+filename '>' + jobdir+filename

我感觉 '>' 搞砸了。但我似乎找不到正确的语法。而且,一旦我正确获得命令,我就可以打印输出变量了吗?

 print '%s' % (output)

输出可能是3页数据..

感谢您的帮助。

这是我的完整代码:

#!/usr/bin/env python

import cgi, os
import cgitb; cgitb.enable()
import subprocess


form = cgi.FieldStorage()
port = form.getvalue("port")
filename = form.getvalue("filename")
directory = form.getvalue("directory")
jobdir = '/var/www/jobs/' + filename


def createdir():
 os.system('mkdir /var/www/jobs/' + filename)
createdir()

def capout():
 output = os.system('/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, directory,     filename, jobdir, filename))
capout()

def htmlout():
 print 'Content-type: text/html\n'
 print '<html>'
 print '<head>'
 print '<title>Capnostic Output</title>'
 print '</head>'
 print '<body>'
 print '<BR><BR><BR><center>'
 print '<table border=0>'
 print '<TR>'
 print '<TD><center>port = %s<BR>filename = %s<BR>Directory = %s<BR>Job Directory = %s</TD>' % (port,filename,directory,jobdir)
 print '</TR>'
 print '</table>'
 print '<BR><BR><BR>'
 print '%s' % (output)
 print '</body>'
 print '</html>'

htmlout()

它现在告诉我:

<type 'exceptions.NameError'>: global name 'output' is not defined 
  args = ("global name 'output' is not defined",) 
  message = "global name 'output' is not defined"

【问题讨论】:

  • print '%s' % (output) 可能是多余的。如果你能做到这一点,你应该能够做到print output
  • 根据official docsThe subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using os function

标签: python os.system


【解决方案1】:

您缺少用于连接字符串和字符串之间的空格的+。您可以使用string formatting 来简化任务,或者在需要的地方添加+ 和空格:

output = os.system('/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, 
                   directory, filename, jobdir, filename))

注意:%s 用于将每个变量视为字符串。

os.system 的使用被 subprocess 模块替换:

sts = os.system("mycmd" + " myarg")
# becomes 
sts = call("mycmd" + " myarg", shell=True)

要捕获输出,您需要使用Popen,将其翻译如下:

def capout():
   cmd = '/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, 
                       directory, filename, jobdir, filename)
   process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   output, error = process.communicate()
   return output

output = capout()

【讨论】:

  • 请注意,它不会为您提供命令的输出,它只会给出返回码,并且用户确实指定了他希望将数据页作为输出。
  • 感谢各位的帮助。我走得更远了.. bvut 现在它告诉我:: global name 'output' is not defined args = ("global name 'output' is not defined",) message = "未定义全局名称“输出””
  • 它是你的范围。当您尝试在 htmlout() 函数中调用它时,output 不存在。
  • 谢谢 RobB 我习惯了 bash。我在代码前面定义了“输出”,你说的是 htmlout() 不知道代码前面已经定义了输出?
  • 您执行了分配变量的函数,但仅针对该函数的本地范围。我已经更新了我的答案以显示如何正确访问它。
【解决方案2】:

您在'&gt;' 之前缺少+

cmd = ('/home/fsoft/cap/capnostic -r' + port + directory + filename + '>' + 
        jobdir + filename)

os.system(cmd)

请注意,os.system 不会返回命令的输出,您可以通过以下方式获得:

import subprocess

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
output, error = proc.communicate()
print output

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 2013-07-03
    • 1970-01-01
    • 2019-09-09
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多