GavinHuttley / tib

Topics in bioinformatics
GNU General Public License v3.0
4 stars 7 forks source link

5.29: Working with files, possible typo, or clarification wanted #232

Closed JackRicho closed 8 months ago

JackRicho commented 8 months ago

5.29: Working with files (end of) first paragraph states:

" Opening a file to read does not return the files contents. "

However, other sources online [1] state that opening a file to read returns whole text, which seem not to agree with the content of the document. If this is a misunderstanding on my part, I would appreciate if an explanation could be added that outlines what opening a file to read does, if not return the files contents.

Thanks

[1] https://www.w3schools.com/python/python_file_open.asp

GavinHuttley commented 8 months ago

This line in the example on the page you reference

f = open("demofile.txt", "r")

is opening a file to be read, as indicated by the "r" argument. If you look at the variable f (or equivalent in another python session), it is not the contents of the file demofile.txt but an object that has a reference to that files location on disk. (It will be something like io.TextIOWrapper.)

The next statement in that example

print(f.read())

is reading the contents of the file. Hope that helps you understand the statement in the notes!