-1

I run into some new problems again. I'm trying to write line by line, according to a condition, into a new file from a file. I can print the lines that I need, but couldn't write it into a file. Here is what I write:

import os

with open('c:\\30001.txt', 'r',encoding= 'utf-8') as lines:
    words_to_copy = set(line.rstrip() for line in lines)
    print(len(words_to_copy))
    #print(filenames_to_copy)

with open('c:\\long.txt', 'r',encoding= 'utf-8') as f:
        for line in f:
              if(line.split(None, 1)[0]) in words_to_copy:
                  with open("c:\\3000line.txt", "w", encoding ='utf-8') as the_file:
                          the_file.write(line) # It runs for minutes not nothing in the new file.
                          #print(line)         #It can print lines that I need.

Many thanks!

Devin Liu
  • 17
  • 3
  • 3
    Either open the file for writing once, or open it each time with ’a’ for append mode. Otherwise it overwrites from the beginning each time. – Peter Wood Dec 22 '19 at 22:01
  • Thank you!. I had tried this mode. .It runs a few minutes.But still, nothing shows up in the 3000line file. – Devin Liu Dec 22 '19 at 22:27
  • Can you provide enough data to run the program? As it stands this isn't reproducible. See: [mcve]. – AMC Dec 23 '19 at 01:17
  • Sorry about that. I'd like to put the sample up here But it's a little too long. I don't know it's ok to put such long sample here. – Devin Liu Dec 23 '19 at 18:08

1 Answers1

1

You are opening the file for writing inside the loop on each iteration. You are doing that using the flag 'w'. From the open docs:

'w': open for writing, truncating the file first

Which means you overwrite the contents after each line.


you have 2 options:

  1. Either use the 'a' flag instead :

    'a': open for writing, appending to the end of the file if it exists

  2. Put both files in the same with statement:

    with open('c:\\long.txt', 'r') as f, open("c:\\3000line.txt", "w") as the_file:
    
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Thanks. I had tried both ways. But the problem remains. It can print all the lines, but couldn't write into the file. – Devin Liu Dec 22 '19 at 22:29
  • Please make sure that you are looking at the same file that you are actually writing to and that there should actually be any content there, meaning that there really are any lines in `long.txt` that start with words from `30001.txt`. Note also that your search is case-sensitive so check that there is no case differences. I am saying all that because I recreated the problem and it worked completely fine @DevinLiu – Tomerikoo Dec 23 '19 at 06:36