-2

Im new to Python, like around an hour and a half into it new.. ive crawled my website using cewl to get a bespoke wordlist for password audits, i also want to combine randomly 3 of these words together.

IE Cewl wordlist ;

word1 word2 word3 word4

using a python script i want to further create another wordlist randomly joining 3 words together IE

word4word2word1 word1word3word4 word3word4word2

so far all ive come up with is;

import random
print(random.choice(open("test.txt").read().split()))
print (random.choice(open("test.txt").read().split()))
print(random.choice(open("test.txt").read().split()))

Whilst this is clearly wrong, it will give me 3 random words from my list i just want to join them without delimiter, any help for a complete novice would be massively appreciated

ReefNerd
  • 3
  • 1
  • Welcome to Stack Overflow! Check out the [tour]. I strongly recommend finishing a Python tutorial before asking here, since this site isn't really built to teach the language basics. Also check out [ask] for tips like how to write a good title. – wjandrea Jan 11 '23 at 20:17

2 Answers2

1

First thing to do is only read the words once and using a context manager so the file gets closed properly.

with open("test.txt") as f:
  lines = f.readlines()

Then use random.sample to pick three words.

words = random.sample(lines, 3)

Of course, you probably want to strip newlines and other extraneous whitespace for each word.

words = random.sample([x.strip() for x in lines], 3)

Now you just need to join those together.

Chris
  • 26,361
  • 5
  • 21
  • 42
0

Using your code/style:

import random
wordlist = open("test.txt").read().split()
randomword = ''.join([random.choice(wordlist), random.choice(wordlist), random.choice(wordlist)])
print(randomword)

join is a method of the string type and it will join the elements of a list using the string as a delimiter. In this case we use an empty string '' and join a list made up of random choices from your test.txt file.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • If there is an error in line 3-4, the file will not be closed. Consider using a context manager. For word sampling, this method is not scalable. – Ci Leong Jan 11 '23 at 20:07
  • Can you loop this, or get this to generate x amount of random 3 word results ? – ReefNerd Jan 11 '23 at 20:09
  • @CiLeong In CPython, after `.read()` the file object no longer exists(There is no reference to it). Garbage collection happens almost immediate, so the file gets closed properly. – S.B Jan 11 '23 at 20:16
  • I see... but isn't the garbage collection only scheduled instead of performed immediately? Wouldn't this mean the close operation might be delayed to a later time? – Ci Leong Jan 11 '23 at 21:00
  • 1
    @CiLeong The one that runs periodically(and in regard to some threshold or something...) is *garbage collector*. It deals with cyclic references. "Reference Counting" is the main mechanism in CPython for destroying zero-referenced objects and as far as I know it happens immediately. – S.B Jan 13 '23 at 16:05