0

I am trying to output a file's contents to terminal using the File.read() function in Python, but keep receiving the following output which doesn't match my ".txt" file contents.

Python Code

from sys import argv
script, input_file = argv

def print_all(f):
    print f.read

current_file = open(input_file)
print "Print File contents:\n"
print_all(current_file)
current_file.close()

Output:

Print File contents:

<built-in method read of file object at 0x1004bd470>
2
  • 3
    you have answered your own question. you just said you are trying to do "File.read()" and yet in your code you do "File.read" Commented Aug 6, 2012 at 14:45
  • 3
    +1 to offset the (in my opinion) unjustified downvote. This is a legitimate question (a common mistake, especially if you come from a Ruby background where the parentheses are optional). Commented Aug 6, 2012 at 14:46

5 Answers 5

5

If you want to call a function, you will need () after the name of the function (along with any required arguments)

Therefore, in your function print_all replace:

print f.read    # this prints out the object reference

with:

print f.read()  # this calls the function
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to change

print f.read

to say

print f.read()

Comments

0

You should do a read()

current_file = open(input_file)
print "Print File contents:\n"
print_all(current_file.read())

Comments

0

You need to actually call the function in your print_all definition:

def print_all(f):
    print f.read()

Comments

0

You haven't called the read method, you've just got it from the file class. In order to call it you have to put the braces. f.read()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.