Question: Problem # 1 Write a python function char _ ind which accepts two parameters, a string and a character ( char ) , which in
Problem #
Write a python function charind which accepts two parameters, a string and a character char which in Python will also have the datatype string The function should return a list with the indices at which the character appears in the string. If the character does not appear in the string, the function should return an empty list.
Function Name: charind
Parameters: string, string
Return Value: list
Example: if I pass the parameters 'Hello World! and l the return value should be However, if I pass the parameters 'Welcome' and a the return value should be
Problem #
Write a python function readcharind which accepts two parameters, a filename string and a character also a string The function should return a list with the indices at which the character appears in the text contained in the file. This index should be based on character count only. If the character does not appear in the file, the function should return an empty list. You MUST call the function from Problem to process the string. If an IOError is thrown when opening the file, the function should return the None value and print an appropriate message. You MUST use a tryexcept control statement, and close the file in a finally clause.
Function Name: readcharind
Parameters: string, string
Return Value: list or None
Example: if I pass the filename 'filetxt and filetxt contains
Hello World!
This is a file
that needs processing and I pass l for the character, the return value should be
If I pass the filename for the same file as the previous example, and pass z for the character, the return value should be
Problem #
Write a python function readcharlines which accepts two parameters, a filename stringand a character also a string The function should return a twodimensional list which contains the number of times the character appears in each line of the file. You MUST call the charind function from Problem to process the string for each line. If an IOError is thrown when opening the file, the function should return an empty list which should NOT be twodimensional If the file exists but is empty, you should also return an empty list. You MUST use a with statement to handle the file
Function Name: readcharlines
Parameters: string, string
Return Value: list
Example: if I pass the filename 'filetxt and filetxt contains
Hello World!
This is a file that needs processing
and I pass l for the character, the return value should be
If I pass the filename for the same file as the previous example, and pass z for the character, the return value should be
Problem #
Write a python function copyfile which copies a file. The function accepts two parameters, the second of which should be optional ie it has a default value The first parameter is a filename string the second parameter is a text string which will be inserted at the start of the copied new file. The default value should be #This is a copy'. The function will return True if the file is successfully created, False if any IO errors are thrown when creating and copying the file. Using file reading and writing, the function should read the file specified by the filename, then create a copy. The name of the new file should be copy where is the name of the original file Hint use string concatenation Insert the second parameter at the top of the contents of the new file with a newline character separating the parameter from the original text
Function Name: copyfile
Parameters: string, optional string
Return Value: boolean
Example: if I pass the filename 'filetxt and pass only the filename, and filetxt contains
Hello World!
This is a file
that needs processing
the new file will have the filename 'copyfiletxt and contains
# This is a copy
Hello World!
This is a file
that needs processing
Problem #
Define a Python function named main. Within the main function, prompt the user to provide, as input, the arguments for each of the functions defined in Problems The main function will call each of the functions with the specified inputs, and display the return value for each function call see examples below
Function Name: Main
Parameters: NA
Return Value: NA
Make sure you call the main function itself at the end of your script. The code should be similar to the code below.
Note: You can assume that all files will be in the same directory as the main python file
Note: Although in both examples we provide the same file for all problems, we should be able to specify different files if
def main:
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
