Short question about using functions

Hello!

I have a question for you about using functions.

If i want to open txt file and store it inside a variable, then I do:

text = open(my_file)

If i want to read the content of the file that I stored inside this variable, then I do:

data = read.text()

How do I know when to use a function with round brackets {open(my_file)} and when with a comma {read.text()}?

Thanks!

It’s a good observation and linked the type of object returned.

Using the built in open() method requires the thing to open as an argument and the way to open, (read, read and write etc.). It returns a file object.

That file object can use other methods using the dot notation, like .read()

So one is using a built in method with the document to open as an argument, and returning it to an object.

The other is taking that object and running methods against it.

Better explained here: https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

2 Likes

Great! I got it!
Thanks!

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.