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.
- 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.
- 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.
-
Create a function for reading in a file
-
Create a function for writing files.
-
Check that the
sampletext.txt
andfile_ops.py
files exist and are present inside the project folder.
You can run thefile_ops.py
file by opening a terminal and executing the following command:python3 file_ops.py
-
Complete the
read_file()
function to read in the sampletext.txt file using theopen
function and return the entire contents of the file. -
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. -
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.
-
Complete the
read_even_numbered_lines()
to return a list of the even-numbered lines of a file (2, 4, 6, etc.) -
Fill in the
read_file_in_reverse()
function to return a list of the lines of a file in reverse order.