In: Computer Science
Using Python 3
Write a program that reads the 4letterwords.txt file and outputs a file called 4letterwords.out that has every word found in 4letterwords.txt but each word on one line. Any leading and trailing blank spaces must be removed in the output file.
Please let me know if anything is required. Please follow the indentation as shown in the code screenshot.
NOTE: I have written two programs , one is to write all words in to a same line of outputfile and other to write each word in a single line. You can use whatever you want.
Program 1 : To write all words in to a same line of outputfile
Code screenshot:
Sample input screenshot:
Sample output:
Copyable code:
f1=open("4letterwords.txt")#opening the input file
f2 = open("4letterwords.out","w") #output file to write the contents
for line in f1: #reading contents of file
line=line.lstrip()#to remove leading spaces
line=line.rstrip()#to remove trainling spaces
f2.write(line+" ") #writing all the words into a single line after
removing leading and trailing spaces
Copyable input:
Rayy
very
Rizu
like
Problem2: To write the each word into a single line
Code screenshot :
Sample input:
Sample output:
Copyable code:
f1=open("4letterwords.txt")#opening the input file
f2 = open("4letterwords.out","w") #output file to write the contents
for line in f1: #reading contents of file
line=line.lstrip()#to remove leading spaces
line=line.rstrip()#to remove trainling spaces
f2.write(line+"\n") #writing each word into a seperate line after
removing leading and trailing spaces
Copyable input:
Rayy
very
Rizu
like