Nick Hofmann
Nick Hofmann

Reputation: 37

Grok Learning Python 3.3 Files

Can somebody please help me with this? Here is what Grok Learning put:

You were working hard writing a letter to your penpal, only to realise that your dog has been "helping" and contributing to the letter too! You notice that every few lines starts with WOOF! and includes things you simply didn't write!

Write a program to read in lines from the file letter.txt and write out a new file, fixed.txt, which contains the only lines that do not start with WOOF!.

For instance, given the following letter.txt:

My vegetable garden is growing really well!
WOOF! Let's play catch!
The tomatoes and cucumbers are nearly ready to eat.
How is your garden going?
WOOF! I better chase that possum!

your program should create the file fixed.txt that contains:

My vegetable garden is growing really well!
The tomatoes and cucumbers are nearly ready to eat.
How is your garden going?

My current code is (I will keep on trying, and editing. Will keep you guys posted):

open("letter.txt").read()
line = letter.txt.split()
if line.startswith("WOOF!"):
  print("")
else:
  print(letter.txt)
letter.txt.close()

Any help would be greatly appreciated. Thanks in advance!

Upvotes: 1

Views: 6051

Answers (6)

HighSpeedNick
HighSpeedNick

Reputation: 1

   with open('fixed.txt', 'w') as f:
      for c in open('letter.txt'):
        if "WOOF!" in c.strip():
          print("", end = "", file=f)
      else:
        print(c.strip(), file=f)

In the task it requires you to open a file to edit. This is done in the first line. You can do:

f = open('fixed.txt', 'w')

instead, however, you need to close the file once you've made the edit with:

f.close()

Then it's just a loop that locates the word "WOOF!" in each line if the "letter.txt". If the word "WOOF!" is located, the "fixed.txt" is written with empty space. Once it has gone through all the lines. The lines left are written into the final "fixed.txt" file.

Upvotes: 0

Edison Bedoya
Edison Bedoya

Reputation: 1

f = open('letter.txt')
archive = open('fixed.txt', 'w')
for line in f:
    lista = line.split() 
    if lista[0] != 'WOOF!': 
        archive.write(line)
        print(line.strip())
f.close()
archive.close()

Upvotes: 0

Noah Nelson
Noah Nelson

Reputation: 11

This is a simpler solution to the question.

f = open('letter.txt')
n = open('fixed.txt', 'w')

for line in f:
  if 'WOOF!' not in line:
    print(line.strip(), file=n)
n.close()
n = open('fixed.txt').read()
print(n)

Upvotes: 1

James Davie
James Davie

Reputation: 11

with open('letter.txt') as fin, open('fixed.txt', 'w') as fout:
  for line in fin:
    if not line.startswith('WOOF!'):
      fout.write(line)

Upvotes: 1

Ben
Ben

Reputation: 1

with open('letter.txt','r') as f:  
with open('fixed.txt','w') as i:

  for line in f:
    if 'WOOF!' not in line:
      print(line.strip(), file=i)

My answer

Upvotes: 0

Matthias
Matthias

Reputation: 13232

OK, you're working on it, so I will give you the solution. You're still not programming but guessing the syntax so you might want to check out the official tutorial.

First we're going to open both files. The default is to open a file for reading. That's OK for the source file, but not for the target file so we use mode='w' to allow writing.

Then we loop over each line in the source file, check if the line starts wirh 'WOOF!' and if it doesn't we're going to write the line to the target file. The loop will stop when all lines from the source file are read.

After the loop we close both files.

def main():
    source_file = open('letter.txt', encoding='UTF-8')
    target_file = open('fixed.txt', mode='w', encoding='UTF-8')

    for line in source_file:
        if not line.startswith('WOOF!'):
            target_file.write(line)

    source_file.close()
    target_file.close()

if __name__ == '__main__':
    main()

If you forget to close the files the target may be empty. If you don't want to handle the closing yourself let Python do this for you with the with statement.

     with open('letter.txt', encoding='UTF-8') as source_file,  open('fixed_alt.txt', mode='w', encoding='UTF-8') as target_file:
         for line in source_file:
             if not line.startswith('WOOF!'):
                 target_file.write(line)

The files will be closed when the with block is left.

Upvotes: 1

Related Questions