CYB 135 Wk 3 – Practice: Reading

0 items
CYB 135 Wk 3 - Practice: Reading
CYB 135 Wk 3 – Practice: Reading
$5.00
  • Description

CYB 135 Wk 3 – Practice: Reading

Accurately complete all reading, Challenge Activities, and Participation Activities to receive full points for this practice. You have unlimited attempts. The points you see in zyBooks do not reflect the points you receive in the gradebook.

The open() built-in function requires a single argument that specifies the path to the file. Ex: open(‘myfile.txt’) opens myfile.txt located in the same directory as the executing script. Full path names can also be specified, as in open(‘C:\\Users\\BWayne\\tax_return.txt’). The file.close() method closes the file, after which no more reads or writes to the file are allowed.

The most common methods to read text from a file are file.read() and file.readlines(). The file.read() method returns the file contents as a string. The file.readlines() method returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on. Both methods can be given an optional argument that specifies the number of bytes to read from the file. Each method stops reading when the end-of-file (EOF) is detected, which indicates no more data is available.

A third method, file.readline(), returns a single line at a time, which is useful when dealing with large files where the entire file contents may not fit into the available system memory.