[Tutor] How to write a function which reads files
Alan Gauld
alan.gauld at yahoo.co.uk
Tue Aug 7 09:35:54 EDT 2018
On 07/08/18 13:46, Rafael Knuth wrote:
> 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
Note that you used a different name here than in your
function definition!
I'm guessing that's a typo, but its one reason to always
cut 'n paste real code into your emails.
Also as a matter of style its usual in Python to make
function names start with a lower case letter:
def myFunction():
or
def my_function():
But that's just a community convention.
But Chris has answered your original question, you
need to add calling parens.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list