2

So, I got this error

    Traceback (most recent call last):
     File **, line 15, in <module>
        del a[del_line]
    IndexError: list assignment index out of range

I am trying to make a program that will generate password combinations, but delete them almost immediately while another program tests them, I just can't seem to get the right code... Here's my coding -

    from __future__ import print_function
    import itertools
    import sys
    from time import sleep 

    del_line = -1
    f1 = open("passfile.txt", "w")
    res = itertools.product("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", repeat=8)
    for i in res:
       print(''.join(i), file=f1)
       sleep(.5)
       del_line += 1
       with open("passfile.txt","r") as textobj:
           a = list(textobj)
       del a[del_line]
       with open("textfile.txt","w") as textobj:
            for n in a:
                textobj.write(n)

Oh, I'm running python 2.7.11 btw

  • You are trying to access the list `a` with an invalid index. You should make sure that `a` isn't empty –  Jun 15 '16 at 07:28
  • You should add `print` statements in your loop to check everything is like you expect. Maybe stop before `a[del_line]` and check the content of `passfile.txt`. – Jérôme Jun 15 '16 at 07:29
  • 1
    `textobj` will be a single string. `list(textobj)` will only have one element. Didi you mean `a.append(textobj)`? – cdarke Jun 15 '16 at 07:32
  • I have checked to make sure it isn't empty, I watched it fill a little. I also tried a.append, it doesn't change anything. – Ryan Spaeth Jun 15 '16 at 15:15

2 Answers2

1

Updated:

from __future__ import print_function
import itertools
import sys
from time import sleep 

f1 = open("passfile.txt", "w")
res = itertools.product("ABCD", repeat=2)
for i in res:
    text = ""
    for string in i: # makes the iterated i 'readable', i is a list with each letter as seperate entry, can be removed
        text += string
    print(text, file=f1)
    f1.flush()
    #sleep(.5)
    f2 = open("passfile.txt", "r")
    a = list(f2)[:-1]
    with open("textfile.txt","w") as textobj:
        for n in a:
            textobj.write(n)

Depending on whether you want the result of i stored as a list or as a string, you should use either AndreyT's code or mine. The trick itself was indeed in f1.flush(). More info on that can be found in this answer: what exactly the python's file.flush() is doing?

Community
  • 1
  • 1
  • I still got the same error even when adjusting to what you suggested :( – Ryan Spaeth Jun 15 '16 at 15:20
  • @RyanSpaeth I have updated my answer and I can confirm this as working. – Ricardo Umans Jun 16 '16 at 12:58
  • I'll have to try it in about two days, I don't have access to a computer at the moment. Edit: Also see what I wrote to the guy below – Ryan Spaeth Jun 17 '16 at 05:24
  • I have tested this, it still doesn't delete the items, which is what I was trying to do in the first place, I have the part down with the actual printing of combinations, that's what the whole beginning "res =.." and "for.." is. – Ryan Spaeth Jun 18 '16 at 02:26
  • Well at least your error is gone, which is what you asked for in the first place ;) If you want to solve the other things, just place prints everywhere in your script and analyse what happens and why it happens. – Ricardo Umans Jun 20 '16 at 12:58
0

Possibly, you should add

f1.flush()

after

print(''.join(i), file=f1)

Explanation: what exactly the python's file.flush() is doing?


(Updated)

from __future__ import print_function
import itertools
import sys
from time import sleep 

f1 = open("passfile.txt", "w")
# Note: count of permutations was reduced, because it takes very long time
res = itertools.product("ABCDEF", repeat=2)
for i in res:
   print(''.join(i), file=f1)
   f1.flush()
   with open("passfile.txt","r") as textobj:
       a = list(textobj)
   del a[-1]
   # Warning: textfile.txt will be overwritten each time
   with open("textfile.txt","w") as textobj:
        for n in a:
            textobj.write(n)
Community
  • 1
  • 1
AndreyT
  • 1,449
  • 1
  • 15
  • 28
  • f1.flush() didn't seem to do anything, I removed all code but the main part and the print and it still didn't do anything :( – Ryan Spaeth Jun 15 '16 at 15:19
  • As I said to the guy above, I will try soon but I don't have access to a computer, and I was playing around on my own with flush, that just fills it with nonsense and creates more text than it normally would, mostly my problem is still being able to delete the actual file, even when it flushed with texted, it failed to delete. – Ryan Spaeth Jun 17 '16 at 05:26
  • I just tried your code, it still doesn't delete any of the combinations, they are just left the way they are printed... (Also in my above comment, when I said delete the file, I meant delete the combinations) – Ryan Spaeth Jun 18 '16 at 02:29