Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Your task is write the following functions: 1. create file o takes filename as a parameter. The function creates an empty file. An example
Your task is write the following functions: 1. create file o takes filename as a parameter. The function creates an empty file. An example is shown below: o create_file("myfile.txt") File myfile. txt should now be visible in the working directory 2. read_file o takes filename as a parameter and returns a string with file content. An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". oread_file("myfile.txt") Output: "This is The Pennsylvania State University" 3. read file lines o This function takes filename as a parameter, and returns a list whose elements are representative of the lines in the file. An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania" and "State University". read_file_lines("myfile.txt") Output: ['This is The Pennsylvania ', 'State University "] 4. write_to_file takes filename and a list as a parameter. This function overwrites the file using list element-by-element where each element is on the newline into the file. It is necessary to check if the list is empty before start inserting elements to file. If the list is empty, print "list is empty" and perform no operations. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". write_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following:- myfile.txt X 1 University Park 2 PA USA 3 5. append_to_file o takes filename and a list as a parameter. This function writes the entire list element-by-element into the file. It is necessary to check if the list is empty before start inserting elements to file. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". append_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following: O myfile.txt X 1 This is The Pennsylvania 2 State University 3 University Park 4 PA USA 5 Return values must be assigned to variables before used. You are NOT allowed to use the following constructs unless specified in the assignment directions. Using the following constructs will result in 0 (zero) for the entire submission (assignment, timed test, etc.). The restricted items are as follows: Concepts not discussed in lectures yet String functions Member functions Exceptions (can use): len () and x=x+[y1, y2,y3] Built-in functions & types Exceptions (can use): str(), readline(), open(), close(), write(), read(), range(),.split() Cannot use .append, .sort, etc. Cannot use "Slicing" Cannot use "list comprehension" Cannot use "not in" (together) Cannot use "in" not allowed with conditionals Exception (can use): with range () Cannot use* and {} Exception (can use): mathematical operations (multiply & exponents) Data type functions Exceptions (can use): int(), str (), float () Break Continue Nested Constructs Exceptions (can use): 2-D list Multiple returns & Multiple assignments Recursion (not allowed unless the question explicitly states otherwise) Functions within functions (Definitions) -- invocation is fine Default Parameters Global variables Keyword as variable names while true or without proper condition
Step by Step Solution
There are 3 Steps involved in it
Step: 1
import string This is the main function which is the function declaration We can call any ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started