Python生成字符组合时内存超限及文件操作异常问题求助
Python生成字符组合时内存超限及文件操作异常问题求助
大家好,我现在遇到了一个特别头疼的问题:用Python生成字符组合时总是触发内存限制错误,进程会中途直接崩溃。我有三段实现类似功能的代码,在模拟器上测试时,哪怕是生成4字符长度的组合,进程跑到大概1/8的进度就直接挂了。我已经把Python升级到64位版本,还调整了电脑的内存设置(设为32000到40000字节,我记得是这个单位),甚至在考虑要不要用任务管理器给Python进程提优先级,但问题还是没解决。
所有帮助我都万分感激!
第一段代码:递归打印字符组合(进程1/8进度崩溃)
这段代码的功能是打印指定字符集的所有组合,但每次运行到大约1/8的进度就会自行终止:
stuff = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]","Backslash","A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "] len=len(stuff) def generate(stuff,i,s,len): if (i == 0): print(s) return for j in range(0,len): appended = s + stuff[j] generate(stuff,i-1,appended,len) return def crack(stuff,len): for i in range(18,19): generate(stuff,i,"",len) crack(stuff,len)
第二段代码:生成组合写入文件(3字符长度时1/8进度崩溃)
这段代码尝试把生成的组合以类表格格式写入文件,但同样在运行到1/8左右时崩溃。我有两个疑问:
- 怎么才能生成更大的输出文件?
- 有没有办法给这个Python进程分配更多内存?
characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "] import itertools combinations = list(itertools.product(characters,repeat=3)) testfileofcombinationsname = "testfile.txt" size_in_bytes=(1024*1024) filename = "combinationsfile" import os def testtxtcombinationsfile(testfileofcombinationsname, size_in_bytes): with open({testfileofcombinationsname}, 'wb') as combinationsfile: {filename}.seek(size_in_bytes - 1) {filename}.write(b'\0') with open(testfileofcombinationsname, 'a') as combinationsfile: for row in combinations: line = " ".join(row) combinationsfile.write(line + '\n') print('A "said" table has been appended and written to testfile.txt!')
第三段代码:写入文件时的seek异常问题
这段代码同样是生成组合写入文件,但我设置了seek(4),结果模拟器显示文件超过4KB,还曾经生成过全是红色高亮圆点的文件。我有几个疑问:
- 为什么seek设置这么小,最终生成的文件却这么大?
- 怎么正确使用
seek来创建指定大小的文件(比如8KB)? - 有没有优化方案,能让这段代码更省内存、运行更稳定?
characters = ["~","!","@","#","$","%","^","&","*","(",")","_","+","`","1","2","3","4","5","6","7","8","9","0","-","=","Q","W","E","R","T","Y","U","I","O","P","{","}","|","q","w","e","r","t","y","u","i","o","p","[","]",'"BackslashCharacter"',"A","S","D","F","G","H","J","K","L",":",'"',"a","s","d","f","g","h","j","k",";","'","Z","X","C","V","B","N","M","<",">","?","z","x","c","v","b","n","m",",",".","/"," "] import itertools combinations = list(itertools.product(characters,repeat=3)) testfileofcombinationsname = "testfile.txt" combinationsfile = open("testfile.txt", "wb") combinationsfile.seek(4) combinationsfile.write(b"\0") with open(testfileofcombinationsname, 'a') as combinationsfile: for row in combinations: line = " ".join(row) combinationsfile.write(line + '\n')
备注:内容来源于stack exchange,提问作者Python Learner




