Python IndexError:列出txt文件时列表赋值索引超出范围

时间:2016-06-15 07:18:20

标签: python python-2.7

所以,我收到了这个错误

    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)

我正在尝试制作一个可以生成密码组合的程序,但是当另一个程序测试它们时几乎立即删除它们,我似乎无法获得正确的代码...... 这是我的编码 -

BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 44.321s
[INFO] Finished at: Wed Jun 15 12:13:27 IST 2016
[INFO] Final Memory: 54M/420M
[INFO] -------------------------------------------------------------------
[ERROR] Failed to execute goal on project oozie-sharelib-oozie: Could not resolve dependencies for project org.apache.oozie:oozie-sharelib-oozie:jar:4.1.0: Failed to collect dependencies for [com.googlecode.json-simple:json-simple:jar:1.1 (compile), org.apache.oozie:oozie-hadoop:jar:2.6.0.oozie-4.1.0 (provided), org.apache.oozie:oozie-hadoop-test:jar:2.6.0.oozie-4.1.0 (test), junit:junit:jar:4.10 (test), org.apache.oozie:oozie-hadoop-utils:jar:2.6.0.oozie-4.1.0 (compile)]: Failed to read artifact descriptor for org.apache.oozie:oozie-hadoop:jar:2.6.0.oozie-4.1.0: Could not transfer artifact org.apache.oozie:oozie-hadoop:pom:2.6.0.oozie-4.1.0 from/to Codehaus repository (http://repository.codehaus.org/): repository.codehaus.org: Unknown host repository.codehaus.org -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :oozie-sharelib-oozie

ERROR, Oozie distro creation failed

哦,我正在运行python 2.7.11 btw

2 个答案:

答案 0 :(得分:1)

<强>更新

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)

根据您是希望将结果存储为列表还是字符串,您应该使用AndreyT的代码或我的代码。诀窍本身确实在f1.flush()。有关这方面的更多信息,请参阅此答案: what exactly the python's file.flush() is doing?

答案 1 :(得分:0)

可能你应该添加

f1.flush()

之后

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

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

(更新)

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)
相关问题