Question

In: Computer Science

For this problem, you'll be writing a script that reads the content of a file, modifies...

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.

Solutions

Expert Solution

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 :-


Related Solutions

Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
Displaying Content of an XML File Using PHP Script or Code In this week, you are...
Displaying Content of an XML File Using PHP Script or Code In this week, you are going to write the PHP script or code to read an XML file and display its content on the screen. The file you will modify is the Products page, which you created in Week 1 Project. Given the following data in XML format, modify the Products page to read and display the information in the Products page: <Product> <Item> <Name>T-Shirt</Name> <Price>12.5</Price> </Item> <Item> <Name>Pants</Name>...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
Implement in Python a script that does the following: 1) reads input from a supposed file...
Implement in Python a script that does the following: 1) reads input from a supposed file called firstnames_2.txt. 2) processes the input and writes and saves the output to a file. NOTE: Please make sure that the names are written in the outfile with one name on each line no comma ( , ) after the name in the output
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
how to create a script file on puTTy script pp1.txt
how to create a script file on puTTy script pp1.txt
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT