Chapter 14
14.1 Reading from files
<_io.TextIOWrapper name='newfile.txt' mode='r' encoding='cp1252'>.
readlines() function is called on fileobj. file_lines is the string list ['Hello world!\n', 'How are you?\n'].
readline() function reads the next line of a file. Since readline() is called right after opening the file the first line is read. The resulting string
"Hello world!"
is printed. Note that readline() includes the
'\n'
character so the print will include an additional newline.
14.2 Writing to files
'w'
indicates write mode, and newlog.txt will be created or overwritten (if newlog.txt already exists) and will allow writing.
close() function must be used.
close() statement finalizes changes to the file.
14.3 Files in different locations and working with CSV files
\n character indicates where line breaks are in a file and is used by the readlines() function to tell lines apart.
csv_read. The first line is
'Title, Author, Pages'
, which is the same as the first row.