# python 3.6+
import sys, random
print('usage: python3 rand9999.py [FILE]')
if '-h' in sys.argv or '--help' in sys.argv:
if len(sys.argv) > 2:
filename = None
if len(sys.argv) == 2:
filename = sys.argv
# Feel free to reply if you can think of a cleaner way to do this.
# Putting 'f = open(..) if.. else..' outside the 'with',
# and then doing "with f as file:" makes it more readable,
# but also seems unsafe, as it could lead to forgetting to
# close files as a general practice.
with (open(filename, 'w') if filename else sys.stdout) as file:
numbers = list(range(10000))
for num in numbers:
It would have been cleaner to use random.sample like @alonsovb
Edit: Updated to use random.sample and ArgParse.
Edit2: I made a post about some different ways to do the file_or_stdout thing. Maybe you can think of a better way.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.