Python Forum
writelines only writes one line to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writelines only writes one line to file
#1
i have a somewhat simple question, that seems to be escaping me. i was messing around with the built-in method itertools to see if i could get anagrams of my name, anyway.. i wanted to write the results out to a file but i only get oneline written out. i tried writelines , i tried moving the with open()
method outside of my for loop but i seem to missing something else.
edit:
i made a edit or two because i had two scripts and i wasnt sure which i copied and pasted , this has the string2 variable from where i was comparing to another for loop, but no big deal still the same issue.

from itertools import permutations

string2 = 'derp'
list1 = []
for i in permutations(string2):
    new = ''.join(i)
    print(new)
with open('file1.txt','w') as f:
    f.writelines(new)
Reply
#2
Maybe something like this:

#! /usr/bin/env python3

from itertools import permutations

string = 'poodle'

with open('test.txt', 'w') as file:
    for mutated in permutations(string):
        file.write(f'{"".join(mutated)}\n')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Dec-05-2021, 09:13 PM)menator01 Wrote: Maybe something like this:

#! /usr/bin/env python3

from itertools import permutations

string = 'poodle'

with open('test.txt', 'w') as file:
    for mutated in permutations(string):
        file.write(f'{"".join(mutated)}\n')

Thanks , this works, I didn’t expect my old laptop to completely freeze while python ran through the permutations of my 11 letter name 😆
Also i just overlooked placing the for loop within the with open() method.
I think that I thought it wouldn’t work. I should have tried it at least.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 395 Feb-15-2024, 11:15 AM
Last Post: rawatg
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,545 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 860 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,368 Sep-27-2022, 01:38 PM
Last Post: buran
  Performance options for sys.stdout.writelines dgrunwal 11 3,108 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,459 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,685 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,222 Jun-29-2021, 09:17 AM
Last Post: Laplace12
Exclamation Why there's a 'blank line' on CSV file? brunolelli 4 3,060 Mar-25-2021, 03:43 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020