= open('mydata.txt', 'r') f
Reading and writing text files
- Files in your OS are basically binary number streams. Programming languages usually make available to you the option to open them as binary numbers or as text.
- If you open them as text, the binary numbers will follow some codification (for example, UTF-8)
Reading from a text file
- The
open()
function returns a file object- If the file is not accessible, Python gives an error
- The second argument, specifies a mode in which the file will be read
r
= (only for) readingw
= (only for) writinga
= append (everything you write will go to the end of the text file)r+
= read and write mode