reading-notes2

https://m7madmomani2.github.io/reading-notes2

View the Project on GitHub M7madMomani2/reading-notes2

FileIO & Exceptions

Image

What Is a File?

At its core, a file is a contiguous set of bytes used to store data. This data is organized in a specific format and can be anything as simple as a text file or as complicated as a program executable. In the end, these byte files are then translated into binary 1 and 0 for easier processing by the computer.

it consist of three main parts:

Header: metadata about the contents of the file (file name, size, type, and so on)

Data: contents of the file as written by the creator or editor

End of file (EOF): special character that indicates the end of the file

Reading Files

Image

These are the methods used in reading files

  • .read(size=-1) : reads from the file based on the number of size bytes, if the value less than 0 or not entered it will read the whole file
  • .readline(size=-1): same as the above but for the lines
  • .readlines() : reads the rest of the lines and return them as a list

Writing Files

These are the methods used in writing files

  • .write(string) : This writes the string to the file.
  • .writelines(seq) : This writes the sequence to the file. No line endings are appended to each sequence item. It’s up to you