【问题标题】:How to read a large file in chunks? [duplicate]如何分块读取大文件? [复制]
【发布时间】:2021-09-26 00:58:49
【问题描述】:

我正在尝试编写一个循环来迭代一个非常大的文件,我正在一个具有 4GB 内存的 linux vm 中编写这个脚本,所以我不能一次加载整个文件我需要读取它的块1024字节(如有错误请指正)

我可以想到两种方法来做到这一点,尽管第二种方法被zsh杀死 因为系统只是用完了内存

第一个不断返回相同的密码列表

wordlist_handler = open('passwords.txt')

def read_wordlist(wordlist_handler):
    return wordlist_handler.read(1024)
    

def read_wordlist2(file='passwords.txt'): 
    with open(file , 'r') as f:
        return list(f)

我的目标是像第一种方法一样遍历文件

if __name__ == "__main__":
    # this will return a list of n passwords 
    
    while True:
        lst = read_wordlist(wordlist_handler)
        
        # use lst in concurrent.futures     
   


【问题讨论】:

  • IT 是一个明文文件 - 逐行读取 - 这样您在任何时候内存中都只有一行。:with open("passwords.txt") as f: for line in f: .... 对该行执行一些操作。不要退回它 - 使用它,如果它不起作用,你会得到下一行......
  • @PatrickArtner 的目标是使用 ``` concurrent.futures ``` 我认为单行在这里没用

标签: python-3.x io


【解决方案1】:

我有这个,但有些密码是 2 位数的长度

while True:
        x = load_chunks()
        print (len(x) , x )
        if len(x) <= 2 :
            break

【讨论】:

  • 第一个没有答案,第二个缺少load_chunks(),第三个为什么要问你是否有解决方案。如果您想向您的问题添加更多信息,请改为edit
猜你喜欢
  • 2019-10-21
  • 2021-08-20
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
相关资源
最近更新 更多