Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Generate ranged numbers and output the result to a file with a newline after each value appended

So what I am trying to do is to generate a list of number values arranging for example from 1 to 100000.

I have created a script that does that, but it only prints the output to the screen:

1
2
n
100000

What I need is exactly the same thing but appended to a file with a newline following each value:

1
2
n
100000 

This is the script that generates the numbers:

def num_gen(min, max):
for i in range(min, max):
    print(i)

print('Enter minimum Appoge value. e.g. 1000')
min_value = int(input())
print("Enter maximum Appoge value. e.g. 3000000")
max_value = int(input())

num_gen(min_value, max_value)

This is the script I wrote in a try to append the output to a file:

def num_gen(min, max):
appoge_list = file('appoge_wordlist.txt', 'w')
for i in range(min, max):
    appoge_list.write(str(i))
    appoge_list('\n')
appoge_list.close()
print('The appoge_list, appoge_wordlist.txt, has been generated. :)')

print('Enter minimum Appoge value. e.g. 1000')
min_value = int(input())
print("Enter maximum Appoge value. e.g. 3000000")
max_value = int(input())

num_gen(min_value, max_value)

Solution: Okay, I have been trying to achieve the above and then tried to oversimplify everything, I let the code explains it all: Here is the new script, accepts no args:

def num_gen():
for i in range(10000, 2000001):
    print(i)
num_gen()

Then run the command: python appoge_generator.py > appoge_wordlist.txt

To run the script from command line you would python appoge_generator.py I just added the > which means append the output of the procedure to the file appoge_wordlist.txt instead of the screen. Now I have a 1990001 lines, 14,2 MB wordlist generated in 0.9 seconds. I hope some one finds this useful.

like image 424
coredumped0x Avatar asked Jan 21 '26 23:01

coredumped0x


1 Answers

It might be faster/more convenient to use Python to write to the file:

with open('appoge_wordlist.txt', 'w') as file:
    for i in range(10000, 2000001):
        file.write('{}\n'.format(i))
like image 76
Farhan Avatar answered Jan 23 '26 11:01

Farhan