简体   繁体   English

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

[英]I'm having trouble printing a file in Python

So I'm trying to open a file and store it's contents as a variable. 因此,我试图打开一个文件并将其内容存储为变量。 It feels like this should be really simple but I am not getting what I want. 感觉这应该真的很简单,但是我没有得到想要的东西。 This is my code: 这是我的代码:

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

And I thought that this would just print out my file. 我认为这只会打印出我的文件。 But I keep getting this: 但我不断得到这个:

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

I could really use some help and fast. 我真的可以快速地使用一些帮助。 thanks a bunch! 谢谢一群!

You need to call the read method, which is done by following it with parentheses. 您需要调用read方法,方法是在其后加上括号。 Without them, it is simply a bound method. 没有它们,它只是一个绑定方法。 So use: 因此使用:

print(DAta.read())

Also, it's a good idea to close a file when you're done accessing it: 另外,最好在访问文件后关闭文件:

DAta.close()

@abby通过包括如read()这样的圆形括号来调用read方法,您应该像DAta.read()这样打印文件内容,并且在读取文件内容之后,一定要养成使用close()方法关闭文件的习惯DAta.close()。

read是File类中的方法 ,因此必须添加()

DAta.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM