Python 2.7,TypeError:';模块';对象不可调用(参考命令行参数)
我已经多次在argparse中使用命令行参数,但我似乎不明白为什么在尝试使用这些参数时会出现TypeError。我已经写了一个脚本,将采取5个必要的参数。我已经包括了下面的脚本。参数值只是几个目录或文件路径。知道我做错了什么吗 以下是回溯:Python 2.7,TypeError:';模块';对象不可调用(参考命令行参数),python,python-2.7,command-line-arguments,main,argparse,Python,Python 2.7,Command Line Arguments,Main,Argparse,我已经多次在argparse中使用命令行参数,但我似乎不明白为什么在尝试使用这些参数时会出现TypeError。我已经写了一个脚本,将采取5个必要的参数。我已经包括了下面的脚本。参数值只是几个目录或文件路径。知道我做错了什么吗 以下是回溯: Traceback (most recent call last): File "C:/program.py", line 186, in <module> sys.exit(builder.main()) File "C:/pr
Traceback (most recent call last):
File "C:/program.py", line 186, in <module>
sys.exit(builder.main())
File "C:/program.py", line 119, in main
filename_config = os.path(self.config)
TypeError: 'module' object is not callable
回溯(最近一次呼叫最后一次):
文件“C:/program.py”,第186行,在
sys.exit(builder.main())
文件“C:/program.py”,第119行,主
filename\u config=os.path(self.config)
TypeError:“模块”对象不可调用
代码如下:
import argparse
import sys
import os
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
class Builder():
def __init__(self):
parser = argparse.ArgumentParser(description='builder: ' + 'version 1.0.0')
parser.add_argument('-s', '--source', help="source dir", type=str)
parser.add_argument('-t', '--target', help="destination dir.", type=str)
parser.add_argument('-l', '--lua', help="path and filename of lua code", type=str)
parser.add_argument('-c', '--config', help="path and filename of configuration", type=str)
parser.add_argument('-d', '--documentation', help="path and filename of documentation", type=str)
args = parser.parse_args()
self.source = args.source
self.target = args.target
self.lua = args.lua
self.config = args.config
self.documentation = args.documentation
if not os.path.isdir((self.target)):
os.makedirs(self.target)
def replace_lua(self, filename, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, filename))
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_version(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_documentation(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def main_XXX(self):
if not os.path.isdir(self.target):
os.makedirs(self.target)
#lua pattern
pattern = "<script><![CDATA[]]></script>"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
read_lua = f.read()
f.closed
subst = "<script><![CDATA[\n" + read_lua + "\n]]></script>"
#version pattern
old_version = "<version>1</version>"
new_version = "<version>2</version>"
#documentation
old_doc = "<documentation></documentation>"
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
read_doc = f.read()
f.closed
read_doc = "<documentation>\n" + read_doc + "\n</documentation>"
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#file_path = os.path.join(subdir, file)
self.replace_lua(file, pattern, subst)
#replace_version(file, old_version, new_version)
self.replace_documentation(file, old_doc, read_doc)
def main(self):
#create expression objects
version = re.compile(r"^(\s*)<version>.*</version>\s*")
mod_date = re.compile(r"^(\s*)<modified>.*</modified>\s*")
lua_script = re.compile(r"^(\s*)<script>.*</script>\s*")
doc = re.compile(r"^(\s*)<documentation>.*</documentation>\s*")
#get version
filename_config = os.path(self.config)
with open(filename_config, 'r') as f:
for line in f:
m_obj = version.search(line)
if m_obj:
new_version = line
else:
m_obj = mod_date.search(line)
if m_obj:
new_mod_date = line + "\n"
f.closed
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
new_doc = f.read()
f.closed
new_doc = "<documentation>\n" + new_doc + "\n</documentation>\n"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
new_lua = f.read()
f.closed
#iterate
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, file))
for line in old_file:
m_obj = version.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_version)
else:
m_obj = mod_date.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_mod_date)
else:
m_obj = doc.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_doc)
else:
m_obj = lua_script.search(line)
if m_obj:
new_file.write(m_obj.group(1) + "<script><![CDATA[\n" + new_lua + "\n]]></script>\n")
else:
new_file.write(line)
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, file))
if __name__ == "__main__":
builder = Builder()
sys.exit(builder.main())
import argparse
导入系统
导入操作系统
进口稀土
从tempfile导入mkstemp
从舒蒂尔进口
从操作系统导入删除,关闭
类生成器():
定义初始化(自):
parser=argparse.ArgumentParser(description='builder:'+'版本1.0.0')
parser.add_参数('-s','-source',help=“source dir”,type=str)
parser.add_参数('-t','-target',help=“destination dir.”,type=str)
parser.add_参数('-l','-lua',help=“lua代码的路径和文件名”,type=str)
parser.add_参数('-c','-config',help=“配置的路径和文件名”,type=str)
parser.add_参数('-d','-documentation',help=“文档的路径和文件名”,type=str)
args=parser.parse_args()
self.source=args.source
self.target=args.target
self.lua=args.lua
self.config=args.config
self.documentation=args.documentation
如果不是os.path.isdir((self.target)):
os.makedirs(self.target)
def replace_lua(自身、文件名、模式、子文件):
#创建临时文件
fh,abs_path=mkstemp()
新建文件=打开(abs\u路径,'w')
old_file=open(os.path.join(self.source,filename))
对于旧_文件中的行:
new_file.write(line.replace(pattern,subst))
#关闭临时文件
新建_文件。关闭()
关闭(fh)
旧的_文件。关闭()
#移动新文件
移动(abs_路径,os.path.join(self.target,文件名))
def replace_版本(自身、文件名、模式、subst):
old_file_path=os.path.join(self.target,文件名)
#创建临时文件
fh,abs_path=mkstemp()
新建文件=打开(abs\u路径,'w')
旧文件=打开(旧文件路径)
对于旧_文件中的行:
new_file.write(line.replace(pattern,subst))
#关闭临时文件
新建_文件。关闭()
关闭(fh)
旧的_文件。关闭()
#删除原始文件
删除(旧文件路径)
#移动新文件
移动(abs_路径,os.path.join(self.target,文件名))
def replace_文档(自身、文件名、模式、subst):
old_file_path=os.path.join(self.target,文件名)
#创建临时文件
fh,abs_path=mkstemp()
新建文件=打开(abs\u路径,'w')
旧文件=打开(旧文件路径)
对于旧_文件中的行:
new_file.write(line.replace(pattern,subst))
#关闭临时文件
新建_文件。关闭()
关闭(fh)
旧的_文件。关闭()
#删除原始文件
删除(旧文件路径)
#移动新文件
移动(abs_路径,os.path.join(self.target,文件名))
def main_XXX(自身):
如果不是os.path.isdir(self.target):
os.makedirs(self.target)
#lua模式
pattern=“”
filename\u lua=os.path(self.lua)
将open(filename_lua,'r')作为f:
read_lua=f.read()
f、 封闭的
subst=“”
#版本模式
旧版本=“1”
新版本=“2”
#文件
old_doc=“”
filename\u doc=os.path(self.documentation)
打开(文件名为“r”)作为f:
read_doc=f.read()
f、 封闭的
read\u doc=“\n”+read\u doc+“\n”
对于os.walk(self.source)中的subdir、dir和文件:
对于文件中的文件:
打印os.path.join(子目录,文件)
#file_path=os.path.join(子目录,文件)
self.replace_lua(文件、模式、子文件)
#替换\u版本(文件、旧\u版本、新\u版本)
自我替换文档(文件、旧文档、已读文档)
def主(自):
#创建表达式对象
version=re.compile(r“^(\s*).*\s*”)
修改日期=重新编译(r“^(\s*).*s*”)
lua_script=re.compile(r“^(\s*).*\s*”)
doc=重新编译(r“^(\s*).*s*”)
#获取版本
filename\u config=os.path(self.config)
将open(filename_config,'r')作为f:
对于f中的行:
m_obj=版本搜索(行)
如果m_obj:
新版本=行
其他:
m_obj=mod_date.search(行)
如果m_obj:
新建日期=行+“\n”
f、 封闭的
filename\u doc=os.path(self.documentation)
打开(文件名为“r”)作为f:
新建文档=f.读取()
f、 封闭的
新建文档=“\n”+新建文档+”\n\n”
filename\u lua=os.path(self.lua)
将open(filename_lua,'r')作为f:
new_lua=f.read()
f、 封闭的
#迭代
对于os.walk(self.source)中的subdir、dir和文件:
对于文件中的文件:
打印os.path.join(子目录,文件)
#创建临时文件
fh,abs_path=mkstemp()
新建文件=打开(abs\u路径,'w')
old_file=open(os.path.join(self.source,file))
对于旧_文件中的行:
m_obj=版本搜索(行)
如果m_obj:
新文件写入(m_对象组(1)+新版本)
其他:
m_obj=mod_date.search(行)