[Tutor] How to write a function which reads files

Rafael Knuth rafael.knuth at gmail.com
Tue Aug 7 08:46:52 EDT 2018


Hi there,

I got this here:

file_path = "C:\\Users\\...\\MyFile.txt" # path shortened for better readability
with open(file_path) as file_object:
    contents = file_object.read()
    print(contents)

It works.

Now I want to convert the code above into a function.
This is what I wrote:

def FileReader(file_path):
    with open(file_path) as file_object:
        contents = file_object.read
        return contents

print(FilePrinter("C:\\Users\\...\\MyFile.txt")) # path shortened for
better readability

I got this error message:

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

How do I fix my function?

Thanks,

Rafael


More information about the Tutor mailing list