1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
SSSSS [86.1K]
4 years ago
13

"Crayon Colors The file ShortColors.txt initially contains the names of all the colors in a full box of Crayola crayons. Write a

program that deletes all colors from the file whose name contains more than six characters."

Computers and Technology
1 answer:
s2008m [1.1K]4 years ago
4 0

Answer:

I am writing the Python program. Since you have not attached the ShortColors.txt file so i am taking my own text containing the names of the colors. You can use this program for your ShortColors.txt file.

infile = "ShortColors.txt"

outfile = "NewFile.txt"

f_read = open(infile)

f_write = open(outfile, "w+")

for name in f_read:

   if len(name)<=7:

       name.rstrip()

       f_write.write(name)

f_read.close()

f_write.close()

Explanation:

I will explain the program line by line.

infile = "ShortColors.txt"  creates an object named infile which is used to access and manipulate the text file ShortColors.txt. This file contains names of colors. This object will basically be used to access and read the file contents.

outfile = "NewFile.txt"  creates an object named outfile which is used to access and manipulate the text file NewFile.txt. This text file is created to contain the colors from ShortColors.txt whose name contains less than or equal to six characters. This means NewFile.txt will contain the final color names after deletion of color names containing more than six characters.

f_read = open(infile)  in this statement open() method is used to open the ShortColors.txt file. f_read is an object. Basically the text file is opened to read its contents.

f_write = open(outfile, "w+")   in this statement open() method is used to open the NewFile.txt file. f_write is an object. Basically this text file is opened in w+ mode which means write mode. This is opened in write mode because after deleting the colors the rest of the colors with less than or equal to six characters are to be written in this NewFile.txt.

for name in f_read:  this is where the main work begins to remove the colors with more than six characters. The loop reads each name from the ShortColors.txt file and here f_read object is used in order use this text file. This loop continues to read each name in the text file and executes the statements within the body of this loop.

if len(name)<=7:  this if condition in the body of for loop checks if the length of the color name is less than or equal to 7. Here each character is counted from 0 to 6 so that is why 7 is used. To check the length of every color name len() method is used which returns the length of each name.

name.rstrip()  this method rstrip() is used to remove the characters from each name whose length is greater than 6.

f_write.write(name)  now write() method is used to write the names of all the colors with less than or equal to six characters in NewFile.txt. For this purpose f_write object is used.

Here i did not remove the colors from the original file ShortColors.txt to keep all the contents of the file safe and i have used NewFile.txt to display color names after deletion of all colors whose name contains more than six characters.

After the whole process is done both the files are closes using close() method.

Attached screenshots are of the program, the ShortColors.txt and NewFile.txt.

You might be interested in
__________ is a protocol used by e-mail clients to download e-mails to your computer.
arlik [135]
POP is a protocol used by e-mail clients to download e-mails to your computer. The correct option among all the options that are given in the question is the last option or option "d". The full form of POP is Post Office Protocol. After the email has been downloaded, the email gets erased from the server automatically.
7 0
4 years ago
2. What are the 3 Alternative software programs that can be used if you don’t have the Microsoft Office programs?
Andrews [41]

Answer:

OpenOffice, Polaris Office, LibreOffice

7 0
3 years ago
Read 2 more answers
Which line of code will only allow a non-decimal point to be stored in a variable? candyCost = float(input("How much is the cand
ad-work [718]

Answer:

candyCost = int(input("How much is the candy?"))

Explanation:

candyCost = input("How much is the candy?") → By default, this how you get input and store the value. And it is stored as a string

candyCost = str(input("How much is the candy?")) → This also gives you a string to store

candyCost = float(input("How much is the candy?")) → This gives you a float number, decimal number, to store

candyCost = int(input("How much is the candy?")) → This gives you an integer number, a non-decimal

4 0
3 years ago
Read 2 more answers
In the SoundByte, the Magic Gradient Pen has an algorithm which listens for ________ and then changes the gradient coloring of t
kirill [66]

Answer:

It listens for sound intensity

Explanation:

It listens for the sound intensity and loudness. As the intensity varies the background color changes as well.

4 0
4 years ago
Read 2 more answers
A variable of type unsigned short stores a value of 0. If the variable value is incremented, what exception will occur?A. No exc
yulyashka [42]

There are different kinds of variable. The answers are below;

  • If the variable value is incremented, therefore, No <u>exception </u>will occur.
  •  If the variable value is incremented, An <u>Overflow </u>will occur
  • If the variable value is decremented no exception will occur.

<h3>What is Underflow?</h3>

Underflow is a known to be a process or exception that happens if a number calculation is too small to be shown by the CPU or memory.

what causes a overflow is the Adding to a variable when its value is at the upper end of the datatype range.

Learn more about variables from

brainly.com/question/24751617

3 0
3 years ago
Other questions:
  • The temperature of a plastic cube is monitored while the cube is pushed 8.7 m across a floor at constant speed by a horizontal f
    8·1 answer
  • What prefix describes a mass that is 1000 times smaller than a gram
    5·2 answers
  • Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has
    8·1 answer
  • Kelly is a college sophomore majoring in computer science. She is interested in gaining exposure to the most useful and current
    13·1 answer
  • Write a function named joinStrings() that keeps asking the user for words and joins them together. Each word should start with a
    12·1 answer
  • In which type of modulation is a 1 distinguished from a 0 by shifting the direction in whichthe wave begins?
    14·1 answer
  • For what reason can security risks never be fully eliminated?​
    7·1 answer
  • Conner has located a research source that is sponsored by a pharmaceutical company. Based on the sponsorship, Conner must be car
    8·1 answer
  • The space between the margin<br> and start<br> of paragraph is called
    8·1 answer
  • Computing is the provision of IT services on demand.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!