【问题标题】:Python 2.7, TypeError: 'module' object is not callable (referring to command-line argument)Python 2.7, TypeError: 'module' object is not callable (指命令行参数)
【发布时间】:2015-02-09 18:50:56
【问题描述】:

我已经多次使用带有 argparse 的命令行参数,但我似乎无法弄清楚为什么在尝试使用这些参数时会收到 TypeError。我编写了一个脚本,它需要 5 个必需的参数。我已经包含了下面的脚本。参数值只是几个目录或文件路径。知道我在这里做错了什么吗?

这是回溯:

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

代码如下:

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())

【问题讨论】:

  • Far 这里的代码太多了。您应该将其减少到实际显示问题的几行。

标签: python python-2.7 command-line-arguments main argparse


【解决方案1】:

pathos 模块中的一个模块。您需要调用它的成员函数。我不知道您到底需要哪一个,但这里有一个os.path 模块文档:https://docs.python.org/2/library/os.path.html

【讨论】:

  • 谢谢。你是对的,它与 os.path 而不是论点本身有关。感谢您抽出宝贵时间帮助我。我只是使用 os.path.abspath(self.config) 代替,因为我直接指向一个文件。
  • 很高兴能帮上忙 :)
猜你喜欢
  • 2018-03-31
  • 2019-04-07
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多