-2

I'm trying to generate random phone numbers. I'd like to output it into a file, but it only gets output once. I've already tried other code but it didn't work either.

import random

a = input(":")
b = input("amount:")
b = int(b)
c = "0123456789"
d = input("prefix:")
d = int(d)
for file in range(b):
    a_file = open("Phone.txt", "w")
    print(
        a,
        d,
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        random.choice(c),
        sep="",
        file=a_file)
    a_file.close()

# var o = print...
#file = open("Phone.txt", "w")
#file_data = repr(o)
#file.write(repr(o))
#file.close
wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

2

Use the following code

a_file = open("Phone.txt", "a")

Instead of

a_file = open("Phone.txt", "w")

a is for appending, w is rewriting the file.

ksohan
  • 1,165
  • 2
  • 9
  • 23