Read a File

  • files are strings that are stored on disk

  • we normally access them by using a path which describes their location

  • to work with the strings inside we use the open() function:

>>> path = 'trivia/questions.csv'
>>> file_handle = open( path, 'r')
>>> file_handle
<_io.TextIOWrapper name='trivia/questions.csv' mode='r' encoding='UTF-8'>
  • we can treat a file like a list of lines (in some ways)

    >>> for line in file_handle:
    ...    print(repr(line))
    'What is RAM?|Memory|Female Sheep|Baby Sheep|Forgotten Sheep\n'
    'What is Python?|Language|Poisonous Snake|French Comedy Troupe|Quebecois Comedy Troupe\n'
    'How many bits in a byte?|8|32|64|4|2|10|100\n'
    'Who was the first Prime Minister of Canada?|Sir John Alexander Macdonald|Lester B. Pearson|Guglielmo Marconi|Avril Lavigne|Pierre Elliott Trudeau\n'
    ...
    >>> for line in file_handle:
    ...    print(repr(line))
    >>> # HUH? why not?
    
  • the file has a position and as you iterate over it that position moves through the file

    • once you reach the end of the file, you can’t (easily) re-iterate over it

Note

repr() produces a code-like “representation” of an object, we use it here so that we can see what the resulting object is clearly.

Exercise: Iterate Over a File

  • Create a new script that opens your questions.txt file and prints every line

  • Why are there blank lines?

  • Modify your script to not show blank lines (hint String Manipulations)