/python_3

Read in data, store, manipulate and output new data to a file

Primary LanguagePython

Lab: Read in data, store, manipulate and output new data to a file

In this lab you'll read the contents of a file and then write the contents to another file.
You'll store the contents of a file into a list so that it can be accessed in different ways.

Tips: Before you Begin

To view your code and instructions side-by-side, select the following in your VSCode toolbar:

  • View -> Editor Layout -> Two Columns
  • To view this file in Preview mode, right click on this README.md file and Open Preview
  • Select your code file in the code tree, which will open it up in a new VSCode tab.
  • Drag your assessment code files over to the second column.
  • Great work! You can now see instructions and code at the same time.

To run your Python code

  • Select your Python file in the Visual Studio Code file tree
  • You can right click the file and select "Run Python File in Terminal" or run the file using the smaller
    play button in the upper right-hand corner of VSCode.
    (Select "Run Python File in Terminal" in the provided button dropdown)
  • Alternatively, you can follow lab instructions which use python3 commands to run your code in terminal.

There are two objectives of this activity:

  1. Create a function for reading in a file

  2. Create a function for writing files.

Exercise Instructions:


  1. Check that the sampletext.txt and file_ops.py files exist and are present inside the project folder.
    You can run the file_ops.py file by opening a terminal and executing the following command:

    python3 file_ops.py 
    
  2. Complete the read_file() function to read in the sampletext.txt file using the open function and return the entire contents of the file.

  3. Complete the read_file_into_line() function so that it returns a data structure of all the contents of the file in a line-by-line sequential order.

  4. Fill in the write_first_line_to_file() that accepts two arguments. This should write only the first line of the file contents into the given output file.

    • Argument 1: The contents of a file to be written
    • Argument 2: The name of an output file.

  5. Complete the read_even_numbered_lines() to return a list of the even-numbered lines of a file (2, 4, 6, etc.)

  6. Fill in the read_file_in_reverse() function to return a list of the lines of a file in reverse order.