T
T
thedoroshchenco2021-11-30 13:29:12
Python
thedoroshchenco, 2021-11-30 13:29:12

How to write data to file once?

Hello!

I execute the following code:

import random
import string

def ipass_generator():
    pwd = string.ascii_letters + string.digits + string.punctuation
    return "".join(random.choice(pwd) for x in range(random.randint(14, 24)))
passwords = (ipass_generator())
print(passwords)

with open('passwords.txt', 'w') as f:
    for password in passwords[:-1]:
        f.write(f'{passwords}\n')
f.write(passwords[-1])


The generated password is written to the file, but it is written as many times as there are characters in the password. How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-11-30
@thedoroshchenco

so what?

...
with open('passwords.txt', 'w') as f:
    f.write(passwords)
# конец файла

there just a string is returned to you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question