Reputation: 21
I am learning Python trough Coursera (Dr. Chuck!) and just finished my first 'useful' personal script outside the homework assignments.
It basically uses two lists of words/digits and creates all possible combinations from those items. I will use this to brute force an old password protected file of which I am sure of the elements (but not the combination).
The script is finally functioning, after hours of fiddling. My question is if this is a 'Pythonic' way to write code. It might be important to learn it the right way from the beginning.
import itertools
beginfile = open('/Users/Mat/Python/combinations/begin.txt')
beginlist = []
for line in beginfile:
line = line.rstrip()
beginlist.append(line)
if line.islower():
capital = line.title()
beginlist.append(capital)
endfile = open('/Users/Mat/Python/combinations/end.txt')
endlist = []
for line in endfile:
line = line.rstrip()
endlist.append(line)
x = itertools.product(beginlist, endlist)
counter = 0
for i in x:
print("".join(i))
counter += 1
print ('TOTAL:', counter, 'items')
Upvotes: 0
Views: 69
Reputation: 1493
import itertools
with open('/Users/Mat/Python/combinations/begin.txt') as beginfile:
beginlist = [line.rstrip().title() for line in beginfile if line.rstrip().islower()]
with open('/Users/Mat/Python/combinations/end.txt') as endfile:
endlist = [line.rstrip() for line in endfile]
x = itertools.product(beginlist, endlist)
data = ["".join(i) for i in x]
print ('TOTAL:', len(data), 'items')
Upvotes: 1