Using data from one file to create two appended lists to a new file
<p>I am trying to take data from one file and create two lists which are both written to a new file. One of the lists contains names 6 characters or less and the second list contains a list with names that do not contain "a" or "e." I have the code done that will form both lists, I have tried them both separately and they work but, I cannot make them both append to a new list at the same time. Whichever list I do first will be the only one that gets appended to the new file. Any help would be much appreciated!</p>

<h1>Code</h1>

<pre><code>main_file = open("words.txt", "r")

#loops to find lists
lists = open('test.txt')
lists.read()
with open("test.txt", "a") as lists:
    for names in main_file:
        if len(names) &lt;= 6:
            lists.write(names)
    line = True
    for line in main_file: 
        if "a" in line or "e" in line or "A" in line:
            line = False
        else:
            lists.write(line)
</code></pre>

<h1>Both lists need to be appended to a new and the SAME file</h1>

<p>If I understand your qn correctly</p>

<pre><code>import re
l1 = []
l2 = []
lists = open('test.txt','w')
with open('words.txt') as f:
    for line in f.read().split('\n'):
        if len(line) &lt;= 6:
            l1.append(line) 
        if not re.search(r'a|e', line):
            l2.append(line) 
lists.write('\n'.join( l1+l2 ) )
lists.close()
</code></pre>

python list file boolean append