Files allow us to store information persistently, even after a program has stopped running, making them an essential tool for managing and organizing data. By practicing file operations, such as reading from and writing to files, you'll gain the foundational skills needed to work with data storage effectively.
Utilize the lab sessions to work on these exercises, and reach out to your Teaching Assistant (TA) or instructor for help. You have unlimited attempts to solve and resubmit these assignments on codePost.io, so use this opportunity to learn, experiment, and improve.
Submission Instructions
For each of the following practice assignments, save your solution in a .py file, with the name being "files" followed by the number of the assignment. For example, for "Files01: Read an Entire File", save your solution in a file named files01.py (notice the lowercase "f"). Then, submit that file to the respective assignment on codePost.io .
There are two types of problems: Required and Optional/Bonus. The Required problems must be submitted to attain full credit for the codePost assignment, while Optional/Bonus problems are provided to help you practice further and deepen your understanding of Python.
To register for a free codePost account, please follow these instructions.
You can submit to codePost multiple times if you want.
Only your last submission will be graded.
From the starting date of this assignment, you have two weeks to complete and submit your solutions.
Watch this short video for a demonstration of submitting an assignment, reviewing the results and resubmitting.
Practice Assignments
Files01: Read an Entire File (Optional/Bonus)
Complete the function, file_read(), to read an entire text file and return a string that contains all the file's contents. The parameter should be the file name.
Note: To read a file, you need to open the file in read mode and save it as a file object. You can then use the read() function on the file object.
Example:
file_read("mytxt.txt") will return 'You just read the file!'
Note: The mytxt.txt file, along with all of the files referenced on this page, are available to use to test your code in codePost.
Provided code:
deffile_read(filename):
Files02: Without Newlines (Optional/Bonus)
Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use a randomly generated filename and file contents), strip off the newline character at the end of each line and return the contents as a single string.
Note, that the newline character is "\n" . There is more than one way to complete this assignment.
Complete the Python function read_third_line() so that it takes a filename and returns the third line of the file as a string. Note, the third line ends with a newline character.
Note: There are multiple ways to write the code to accomplish this task.
Example:
read_third_line("afile.txt") returns 'LOL, you get the third line
'
Provided code:
defread_third_line(filename):
Files04: Append to a File (Optional/Bonus)
Complete the function append_text() to append the string, "I appended it" to the file, filename.
Note that you will need to open the file in the appropriate mode.
Provided code:
defappend_text(filename):
Files05: Reading and Storing (Required)
Complete the function read_filetolist() so that it takes the name of a file, reads that file and stores each line into a different element in a list, then returns the list.
Example:
read_filetolist("myf.txt") will return: ['Fall break\n', 'Thanksgiving\n', 'Christmas is coming\n']
Provided code:
defread_filetolist(filename):
Files06: Counting Lines (Optional/Bonus)
Complete the Python function count_lines() so that it take a filename, reads the file's contents, counts the number of lines in the file and returns the number of lines.
Complete the function write_list() to take a filename and a list. Write each element in the list to the file. Example: colors = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow'] write_list( "listFile.txt", colors )
will produce the following file: listFile.txt
Red
Green
White
Black
Pink
Yellow
Provided code:
defwrite_list(filename, lst):
Files08: Random Line (Optional/Bonus)
Complete the function read_r_line(filename, lineNum) to take a filename and a line number and return the contents of that line number from the file as a string. Note, the line index is one less than the line number.
Hint: The code from Files03 will be helpful.
Examples:
read_r_line("afile.txt", 1) returns 'This the first line '
read_r_line("afile.txt", 3) returns 'LOL, you get the third line '
Provided code:
defread_r_line(filename, lineNum):
Files09: Search in a file (Required)
Complete the function search_file(filename, word) to take a filename and a word. If the the word is found in the file contents, the function should return True, otherwise it should return False.
Note that the function should return the Boolean True / False NOT strings "True" / "False" . Also make sure you handle differences in capitalization. DoG and dog should both count as True for DOG.
Complete the function compare_files(filename1, filename2) so that it takes two file names and if their content is exactly the same return True, otherwise return False.
For this assignment, you must use the read() method.
Note: Capitalization, punctuation and spacing must also be the same.
Write a function open_if_exists() that takes a file name, attempts to open it, and returns the contents.
If the file does not exist then return the reserved word None.
Provided Code: