Question

You must write a function that works like a small editor. It should open an existing...

You must write a function that works like a small editor. It should open an existing file (let us say this is File1.txt. This file must exist before the function is run), read it line by line. Then write it to another file (let us say this is File2.txt. This file need not exist since Python will create it). However, if a certain line exists in the first file, it should be replaced by another line supplied by you. You must submit the source file and code. I do not need the destination file since that will be created by your code.

Hint of rough outline of code:

You must create a text file with Notepad and save it as File1.txt or you can call it name_source, e.g. Sanford_source.txt. Write whatever you want into this file. You can copy and paste anything you find in the internet provided it is simple text. Throughout this file add a signature line that will be replaced. E.g.

dan_source.txt may have to following content:

'Once upon a time there lived a lovely princess with fair skin and blue eyes.

She was so fair that she was named Snow White.

Dan line that must be edited out.

Her mother died when Snow White was a baby and her father married again.

This queen was very pretty but she was also very cruel.

Dan line that must be edited out.

The wicked stepmother wanted to be the most beautiful lady in the kingdom.

She would often ask her magic mirror, “Mirror! Mirror on the wall! Who is the fairest of them all?”

The magic mirror would say, “You are, Your Majesty!”

Dan line that must be edited out.

But one day, the mirror replied, “Snow White is the fairest of them all!”

The wicked queen was very angry and jealous of Snow White.

She ordered her huntsman to take Snow White to the forest and kill her.

Dan line that must be edited out.

“I want you to bring back her heart,” she ordered.

The huntsman took pity on Snow White and set her free.

Dan line that must be edited out.

He took a deer's heart to the wicked queen and told her that he had killed Snow White.

Dan line that must be edited out.

Snow White wandered in the forest all night, crying.'

In several places in the above file I added “Dan line that must be edited out’ You should add a similar sentence with your own name in seven or more places in your source file. This line must be replaced by ‘Dan replaced the original line here’ again with your name instead of Dan. Call the file that you write into with the replacement Dan_dest.txt (again with your name).

Your source and destination files must be in different directories on your computers and not in the current working directory of Python. That means when you are reading in the source and writing the destination file, you must provide full paths

E.g. I may write code as

def dan_edit(str1, str2):

infil= open('C:/DataAnalysis/sample1/chel_source.txt','r')

outfil=open('C:/mydocs/samples/chel_dest.txt','w')

where srt1 is the line that must be edited out (i.e. Dan line that must be edited out) and str2 is the line that will be put in its place (i.e. Dan replaced the line here).

You should use the line function to read the source file line by line e.g.

for line in infil: --> this will read each line in the source and check if the line that was read is similar to the one that should be substituted, you must use the replace attribute of the line function

repline = line.replace(str1,str2) ---> this will replace str1 with str2, if the line that was read happens to be similar to str1. This code will execute only if the line that was read is similar to str1. If it is, then it will replace with str2 and assign it to repline. If the line is not similar to str1, then the line is left as it is.

Next this must be written into the destination file

outfil.write(repline)---> this will write the line into the destination file. Remember all of this is a part of the for loop.

You will call this function i.e. use it at the Spyder prompt with the following function call: dan_edit(‘Dan line that must be edited out.’, ‘Dan replaced the line here’)

Note that I used forward slash in the file paths. Python uses forward slashes not backslash that windows uses.

NB: Please follow hint format and provide a picture of spider code

Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
Description: You will be creating a program extractIP.py which will read from a file called wireShark.txt...
Description: You will be creating a program extractIP.py which will read from a file called wireShark.txt and extract all the ip addresses which can be of the format: 1,2 or 3 digits. 1, 2 or 3 digits.1, 2, or 3 digits. 1,2,3 digits Here are some examples of valid IPv4 addresses: 10.0.0.1, 12.123.123.123, 126.255.255.254, 191.255.255.254, 192.168.1.1, 239.255.255.255 And you will output them to another file called IPAddresses.txt , one per line, listing source and destination. Example: Source Destination 12.123.123.123 123.123.123.123...
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code which will read in strings from nemo.txt, then call Lab08(string) on each line of nemo.txt. Your job is to write Lab08.m. The Lab08 function will take in a string as input and will return an integer n as output. The value of n will be the nth word in the input string, where n is the location of the substring 'Nemo'. Please note that...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Write a program in python that reads in the file quiztext (txt) and creates a list...
Write a program in python that reads in the file quiztext (txt) and creates a list of each unique word used in the file, then prints the list. Generate a report that lists each individual word followed by the number of times it appears in the text, for example: Frequency_list = [('was', 3), ('bird',5), ('it', 27)….] and so on. Note: Notice the structure: a list of tuples. If you're really feeling daring, Google how to sort this new list based...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...