简体   繁体   中英

How to write line by line into a file in a loop using python?

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!

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM