我在使用Python打印文件时遇到问题

时间:2016-06-06 05:05:12

标签: python python-3.x

所以我试图打开一个文件并将其内容存储为变量。感觉这应该很简单,但我没有得到我想要的东西。 这是我的代码:

file = str(input('enter a file name to decipher: '))
DAta = open(file,'r')
print(DAta.read)

我认为这只会打印出我的文件。但我一直这样说:

<built-in method read of _io.TextIOWrapper object at 0x03044E30>

我真的可以快速使用一些帮助。谢谢你!

3 个答案:

答案 0 :(得分:1)

您需要调用read方法,这是通过括号括起来完成的。没有它们,它只是一种绑定方法。所以使用:

print(DAta.read())

此外,在您访问文件时关闭文件也是一个好主意:

DAta.close()

答案 1 :(得分:0)

@abby调用read方法通过包括像这个read()的圆形paranthesis,你应该像这个DAta.read()一样打印文件的内容,并在读完文件的内容之后,总是养成关闭文件的习惯()方法DAta.close()。

答案 2 :(得分:0)

读取是来自File类的方法,因此您必须添加()

<h:form>
  <div class="container">
    <div class="section">

      <!--   Icon Section   -->
      <h:panelGroup id="result" styleClass="row">
        <ui:repeat var="project" value="#{homeBean.displayProjectList}">

          <div class="col s12 m6 l4">
            <div class="card">
              <div class="card-image">
                <img src="resources/materialize/background1.jpg" />
                <span class="card-title">#{project.title}</span>
              </div>
              <div class="card-content">
                <p>#{project.summary}</p>
              </div>
              <div class="card-action">
                <a href="./event.xhtml">More</a>
              </div>
            </div>
          </div>

        </ui:repeat>
      </h:panelGroup>


      <div class="row center">
        <h:commandButton styleClass="btn-large yellow lighten-2 black-text" value="SHOW MORE" actionListener="#{homeBean.showMore()}" >
          <f:ajax render="result" />
        </h:commandButton>

      </div>

    </div>
  </div>
</h:form>