In: Computer Science
For this problem, you'll be writing a script that reads the content of a file, modifies that content, and then writes the result to a new file.
a. Define a function called modified() that takes a string and returns a "modified" version of the string. The type of modification is up to you (be creative!), but I have a couple requirements: You must make at least two different modifications, and at least one of those modifications must be some kind of replacement. (I recommend making use of the string method .replace() link). You need to briefly explain what your modifications are in the docstring. Put this function in the same script as in part (b).
So for example you could replace every occurrence of the letter "O" with the digit "0" and every occurrence of the letter "I" with the digit "1" (that's 2 modifications, both replacements).
Or alternatively, you could make everything uppercase (modification 1, not a replacement) and then replace every occurrence of the word "SMALL" with the string "small" (modification 2 , a replacement).
b. Write a script called filechanger.py that asks the user to type in a source filename and a destination filename, opens both files, loops through the contents of the source file line-by-line (using modified() to modify each line and writing the resulting line to the destination file), and then closes both files (using with-as).
If an OSError occurs, the script should print a message saying that there was an error working with one of the files and ask the user to enter in the filenames again. You may implement this with either a while loop with .readline(), or a for loop, but the script must loop through the source file line-by-line; you may not use .read() or .readlines().
*Use with-as insted of .close() when working with files and every command that works with files should be insidea a try block.
filechanger.py :-
# Function to modify string and return modified string
def modified(line_string):
"""
Convert string into upper case then replace all occurrence of S by $, all occurrence of L by 1, and
all occurrence of O By 0
:param line_string: a string
:return: modified string
"""
line_string = line_string.upper() # convert string to uppercase and store result string in same variable.
line_string = line_string.replace('S', '$') # replace all occurrence of S by $
line_string = line_string.replace('L', '1') # replace all occurrence of L by '1'
line_string = line_string.replace('O', '0') # replace all occurrence of O bt '0'
return line_string # return modified string
# Program Entry point
if __name__ == '__main__':
# Execute entire process inside while loop
# so that if OSError occur then continue while loop else terminate loop
while True:
# Prompt for source and destination file
source_file = input("Enter source file: ")
destination_file = input("Enter destination file: ")
# perform file related task inside try
# and catch OSError if it occur
try:
# Opening both source and destination file using with as
# here source file will open in read mode and destination file will open in write mode
with open(source_file, "r") as source, open(destination_file, "w") as destination:
# loop through each line in source file
for line in source:
# write destination file with modified string
destination.write(modified(line))
# catch OSError
except OSError:
# if OSError occur then this block execute
# this block print a message and continue while loop
print("There was an error while working with file(s)")
continue
else:
# if OSError doesn't then this block executes terminate while loop using break keyword
break
Sample Outputs :
Screenshot of file.txt :-
Screenshot of dest.txt :-
Please refer to the Screenshot of the Code given below to understand indentation of Python code :-