类型错误:';str';对象不可调用(Python 2.4.3)
在Python 2.4.3中尝试运行以下脚本时,出现错误:类型错误:';str';对象不可调用(Python 2.4.3),python,Python,在Python 2.4.3中尝试运行以下脚本时,出现错误: [root@localhost bin]# ./clokins.py Traceback (most recent call last): File "./clokins.py", line 162, in ? print main(argv) File "./clokins.py", line 156, in main cloc = cloc_cmdline(fpath, arguments[1:]) F
[root@localhost bin]# ./clokins.py
Traceback (most recent call last):
File "./clokins.py", line 162, in ?
print main(argv)
File "./clokins.py", line 156, in main
cloc = cloc_cmdline(fpath, arguments[1:])
File "./clokins.py", line 118, in cloc_cmdline
(binary, cloc_opts) = readopts(cmdarg)
File "./clokins.py", line 108, in readopts
exit('File does not exists : %s'%(options.clocpath))
TypeError: 'str' object is not callable
资料来源:
克罗金斯
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012 Rodolphe Quiedeville <rodolphe@quiedeville.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from os import path, access, X_OK
from os import chdir
from sys import argv
from commands import getoutput
from optparse import OptionParser
VERSION = "1.2.0"
def trigger_start(string):
"""
Return true if we can begin to count
"""
if string.startswith('language,filename,blank,comment,code'):
return True
else:
return False
def trigger_stop(string):
"""
Return true if we have to stop counting
"""
if string.startswith('files,language,blank,comment,code'):
return True
else:
return False
def lang(string):
"""
Return then language name formatted
"""
langname = string.lower()
if langname == 'bourne shell':
langname = 'shell'
return langname
def namedir(string):
"""
Return the name dir "a la sloccount"
"""
if string.startswith('/'):
nmd = path.dirname(string).split('/')[1]
else:
nmd = path.dirname(string).split('/')[0]
if nmd == '.':
nmd = 'top_dir'
return nmd
def load_exclude(filename):
"""
Look if an exlude file is present
"""
optname = '--exclude-list-file'
if path.isfile(filename):
return '%s=%s' % (optname, path.abspath(filename))
else:
return ""
def readopts(cmdargs):
"""
Read options passed on command line
"""
opts = ""
parser = OptionParser()
parser.add_option("--exclude-list-file",
action="store",
type="string",
dest="exclude_filelist",
default=None)
parser.add_option("--binary",
action="store",
type="string",
dest="clocpath",
default="/usr/bin/cloc")
options = parser.parse_args(args=cmdargs)[0]
if options.exclude_filelist is not None:
opts = load_exclude(options.exclude_filelist)
if options.clocpath is not None:
if not path.isfile(options.clocpath):
exit('File does not exists : %s'%(options.clocpath))
if not access(options.clocpath, X_OK):
exit('File does not exists : %s'%(options.clocpath))
return options.clocpath, opts
def cloc_cmdline(fpath, cmdarg):
"""
Build the cloc command line
"""
(binary, cloc_opts) = readopts(cmdarg)
cmdline = "%s --csv %s --by-file-by-lang %s %s""" % (binary,
'--exclude-dir=.git',
cloc_opts,
fpath)
return cmdline
def parse_cloc(text):
"""
Parse the cloc output
"""
flag = False
output = ""
for line in text.split('\n'):
if trigger_stop(line):
flag = False
if flag:
datas = line.split(',')
output += '%s\t%s\t%s\t%s\n' % (datas[4],
lang(datas[0]),
namedir(datas[1]),
datas[1])
if trigger_start(line):
flag = True
return output
def main(arguments):
"""
Main function
"""
fpath = arguments[len(arguments) - 1]
if path.isdir(fpath):
chdir(fpath)
fpath = '.'
cloc = cloc_cmdline(fpath, arguments[1:])
text = getoutput(cloc)
return parse_cloc(text)
if __name__ == '__main__':
print main(argv)
#/usr/bin/env python
#-*-编码:utf-8-*-
"""
版权所有(C)2012鲁道夫·奎德维尔
此程序是免费软件:您可以重新发布和/或修改它
它是根据GNU通用公共许可证的条款发布的
自由软件基金会,或者许可证的第3版,或者
(由您选择)任何更高版本。
这个节目的发布是希望它会有用,
但没有任何保证;甚至没有对
适销性或适用于特定用途。请参阅
有关更多详细信息,请参阅GNU通用公共许可证。
您应该已经收到GNU通用公共许可证的副本
与此程序一起使用。如果没有,请参阅。
"""
从操作系统导入路径,访问,X_确定
从操作系统导入chdir
从系统导入argv
从命令导入getoutput
从optpasse导入OptionParser
VERSION=“1.2.0”
def触发器_启动(字符串):
"""
如果我们可以开始计数,则返回true
"""
if string.startswith('language,filename,blank,comment,code'):
返回真值
其他:
返回错误
def触发器_停止(字符串):
"""
如果必须停止计数,则返回true
"""
if string.startswith('files,language,blank,comment,code'):
返回真值
其他:
返回错误
def lang(字符串):
"""
然后返回格式化的语言名称
"""
langname=string.lower()
如果langname=='bourne shell':
langname='shell'
返回langname
def namedir(字符串):
"""
返回名称dir“a la sloccount”
"""
如果string.startswith('/'):
nmd=path.dirname(string.split('/')[1]
其他:
nmd=path.dirname(string.split('/')[0]
如果国家导弹防御系统=='':
国家导弹防御系统=‘最高指挥’
归还nmd
def load_exclude(文件名):
"""
查看是否存在排除文件
"""
optname='--排除列表文件'
如果路径为.isfile(文件名):
返回“%s=%s%”(optname,path.abspath(文件名))
其他:
返回“”
def readopts(cmdargs):
"""
读取在命令行上传递的选项
"""
opts=“”
parser=OptionParser()
parser.add_选项(“--exclude list file”,
action=“store”,
type=“string”,
dest=“排除文件列表”,
默认值=无)
parser.add_选项(“--binary”,
action=“store”,
type=“string”,
dest=“clocpath”,
default=“/usr/bin/cloc”)
options=parser.parse_args(args=cmdargs)[0]
如果options.exclude_文件列表不是None:
opts=load\u exclude(options.exclude\u文件列表)
如果options.clocpath不是None:
如果不是path.isfile(options.clocpath):
退出('文件不存在:%s'(options.clocpath))
如果不访问(options.clocpath,X_OK):
退出('文件不存在:%s'(options.clocpath))
return options.clocpath,opts
def cloc_cmdline(fpath,cmdarg):
"""
构建cloc命令行
"""
(二进制,cloc_opts)=readopts(cmdarg)
cmdline=“%s--csv%s--by-file by-lang%s%s”“”%(二进制,
'--exclude dir=.git',
cloc_选择,
fpath)
返回命令行
def parse_cloc(文本):
"""
解析cloc输出
"""
flag=False
output=“”
对于text.split('\n')中的行:
如果触发停止(第行):
flag=False
如果标志:
数据=line.split(',')
输出+='%s\t%s\t%s\t%s\t%s\n'(数据[4],
lang(数据[0]),
namedir(数据[1]),
数据[1])
如果触发器启动(第行):
flag=True
返回输出
def main(参数):
"""
主要功能
"""
fpath=arguments[len(arguments)-1]
如果路径为isdir(fpath):
chdir(fpath)
fpath='。'
cloc=cloc_cmdline(fpath,参数[1:])
text=getoutput(cloc)
返回parse_cloc(文本)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印主屏幕(argv)
但是在Python2.7.3上运行相同的脚本时不会出现错误。您使用的
exit
内置代码没有文档记录,不应该使用,并且在不同的Python版本上的行为显然不同。您应该使用适当的print
语句和sys.exit
。准确地说,它“对交互式解释器外壳有用,不应在程序中使用。”