【问题标题】:Hashing wordlist with big input output files in Python 3.8在 Python 3.8 中使用大输入输出文件散列词表
【发布时间】:2020-12-19 04:09:36
【问题描述】:

我是编码初学者,我正在尝试构建一个脚本,该脚本将 txt 文件作为输入,对其进行哈希处理并输出到另一个 txt 文件,该文件的每一行都包含“string:hashedstring”。代码工作正常。我现在面临的问题是,如果输入文件很大,它将消耗所有 RAM 并杀死它。我尝试使用块,但不知道如何将它与多行输入和输出一起使用。 非常欢迎任何关于此处主要主题以外的代码其他部分的建议,因为我才刚刚开始。谢谢。

import argparse
import hashlib
import os
import sys

def sofia_hash(msg):
    h = ""
    m = hashlib.md5()
    m.update(msg.encode('utf-8'))
    msg_md5 = m.digest()
    for i in range(8):
        n = (msg_md5[2*i] + msg_md5[2*i+1]) % 0x3e
        if n > 9:
            if n > 35:
                n += 61
            else:
                n += 55
        else:
            n += 0x30
        h += chr(n)
    return h

top_parser = argparse.ArgumentParser(description='Sofiamass')
top_parser.add_argument('input', action="store", type=argparse.FileType('r', encoding='utf8'), help="Set input file")
top_parser.add_argument('output', action="store", help="Set output file")
args = top_parser.parse_args()


sofiainput = args.input.read().splitlines()
a = 0
try:
    while a < len(sofiainput):
        target_sofiainput = sofiainput[a]
        etarget_sofiainput = (target_sofiainput).encode('utf-8')
        try:
            sofia_pass = sofia_hash(target_sofiainput)
            x = True
        except KeyboardInterrupt:
            print ("\n[---]exiting now[---]")
        if x == True:
            with open(args.output, 'a') as sofiaoutput:
                sofiaoutput.write(str(target_sofiainput) + ":" + str(sofia_pass) + "\n")
        elif x == False:
           print('error')

        a += 1
except KeyboardInterrupt:
    print ("\n[---]exiting now[---]")
except AttributeError:
    pass

【问题讨论】:

    标签: python python-3.x input hash output


    【解决方案1】:

    当您使用 open 命令打开文件时,它会创建一个称为文件处理程序的对象。所以,当你这样做时:

    with open('filepath.txt', 'r') as f:
        for line in f:
            print(line)
    

    它只将您正在使用的当前行保留在 RAM 中,从而实现您使用尽可能少的 RAM 的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 2014-09-11
      • 2012-11-05
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多