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()
Step-by-step 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.