Reading Files

In this page I describe how to access a file for reading in Python. There are various ways we could address this, but in conversation with the staff, we concluded the following would be simplest for everyone, while also teaching a bit about the important cd command in the terminal.

Also available: Krithika Venkatasubramanian made a video explanation of the steps below.

  1. ALWAYS open your EECS_183 folder in VS Code - not some subfolder like lab_4, etc. If you’re unsure what I’m talking about, please reread this section of tutorial 3. This is very important. If you open a different folder in VS Code then various things will go subtly, and sometimes not-so-subtly, wrong, and it will affect you repeatedly throughout the semester.
  2. When you’re ready to run your code, click the triangle button as usual.
  3. You may receive a FileNotFoundError, looking something like this:

     Traceback (most recent call last):
     File "c:\Users\Steve\Documents\EECS_183\labs\lab_4\public_tests.py", line 174, in <module>
         run_all_tests()
         ~~~~~~~~~~~~~^^
     File "c:\Users\Steve\Documents\EECS_183\labs\lab_4\public_tests.py", line 168, in run_all_tests
         test_build_game_dictionary()
         ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
     File "c:\Users\Steve\Documents\EECS_183\labs\lab_4\public_tests.py", line 82, in test_build_game_dictionary
         full = games.build_game_dictionary('games.csv')
     File "c:\Users\Steve\Documents\EECS_183\labs\lab_4\games.py", line 41, in build_game_dictionary
         with open(csv_file_name, 'r', encoding='utf-8') as csv_file:
             ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    # primer-spec-highlight-start
     FileNotFoundError: [Errno 2] No such file or directory: 'games.csv'
    # primer-spec-highlight-end
    
  4. If you get such an error, then next look at the prompt in the terminal, just below the error message. My terminal prompt is shown below; yours may look different in a few ways, but it should show an indication that you are in your EECS_183 folder (not a subfolder).

     (.venv) Steve@DESKTOP-CDBBVD0:~/Documents/EECS_183
     $
    
    • If you don’t see an EECS_183 folder indication, please reread this section to make sure that you’re opening your EECS_183 folder in VS Code.
    • If you need a reminder about terminals, please check this section of tutorial 2.
  5. We need to figure out what path describes the location of the file you want to read. Find it in the EXPLORER area in the left tab of VS Code.
    • For example, for lab 4, for the file games.csv, we need the path labs/lab_4.
    • That is, games.csv is in the EECS_183 folder, in the labs sub-folder, in the lab_4 sub-sub-folder.
    • So the final format of the path we need is basically sub_folder/sub_sub_folder - with a forward slash in between.
  6. Click in the terminal area so you can type at the prompt.
  7. Type cd followed by a space, followed by the sub_folder/sub_sub_folder path to the file you want to read.

    • For example, for lab 4, you would type cd labs/lab_4
  8. Then hit enter. This will change your current directory to the desired sub-sub-folder (e.g., lab_4), where the file you want to read (e.g., games.csv) is located. You should see some indication that you’re in the correct sub-sub-folder in your prompt. Again, your prompt may look a bit different from mine, but here’s mine as an example, for lab 4. Compare / contrast to the terminal prompt from before.

     (.venv) Steve@DESKTOP-CDBBVD0:~/Documents/EECS_183/labs/lab_4
     $
    
    • cd stands for “change directory”.
  9. Now, when you hit the arrow to run your code, the Python interpreter will be able to find the file (e.g., games.csv) and you should not get a FileNotFoundError anymore.

© 2026 Steven Bogaerts.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Creative Commons License

All materials provided for this course, including but not limited to labs, projects, notes, and starter code, are the copyrighted intellectual property of the author(s) listed in the copyright notice above. While these materials are licensed for public non-commercial use, this license does not grant you permission to post or republish your solutions to these assignments.

It is strictly prohibited to post, share, or otherwise distribute solution code (in part or in full) in any manner or on any platform, public or private, where it may be accessed by anyone other than the course staff. This includes, but is not limited to:

To do so is a violation of the university’s academic integrity policy and will be treated as such.

Asking questions by posting small code snippets to our private course discussion forum is not a violation of this policy.