【问题标题】:File iteration issue in python [duplicate]python中的文件迭代问题[重复]
【发布时间】:2020-12-12 11:48:17
【问题描述】:

我有一个分配,其中我有两个文件,即“user.txt”,“pwd.txt”现在我想基本上迭代这些文件的每个值,即“John”,所有值都在“pwd.txt”中。 txt”,然后是“user.txt”中的另一个值,所有值都是“pwd.txt”。

这里我要实现线程。

这是我的代码,

import threading


f1 = open("user.txt", "r")
f2 = open("pwd.txt", "r")
threads = []
def brute():

    for letter in f1.readlines():
        print "[+]First Value: {}".format(letter)
        for second_letter in f2.readlines():
            print "[+]Second Value: {}".format(second_letter)
        
            threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))


for thread in threads:

    thread.start()

for thread in threads:

    thread.join()
    
def runner(word1,word2):

    print("[+]I am just a worker class: {0}:{1}".format(word1,word2))

输出如下

[+]First Value: john

[+]Second Value: test1234

[+]Second Value: socool!

[+]Second Value: abcde1234

[+]Second Value: password1

[+]Second Value: Passw0rd

[+]Second Value: adminRocks

[+]First Value: dean

[+]First Value: harry

[+]First Value: sam

[+]First Value: joseph

[+]First Value: rick

[+]I am just a worker class: john:test1234

[+]I am just a worker class: john:socool!

[+]I am just a worker class: john:abcde1234

[+]I am just a worker class: john:password1

[+]I am just a worker class: john:Passw0rd

[+]I am just a worker class: john:adminRocks

[Finished in 0.3s]

我不确定如何在此处打印所有用户值以及密码文件中的所有密码。非常感谢任何帮助。

【问题讨论】:

    标签: python multithreading python-2.7


    【解决方案1】:

    根据您的帖子,目标是交叉加入用户和密码文件。为此,在 pwd 文件中加载一次,然后处理每个用户(每个线程一个)。

    请注意,我使用 Python 3.8 对此进行了测试。

    试试这个代码:

    import threading
    
    # create files for testing
    user = "user1\nuser2\nuser3\nuser4\nuser5\nuser6\nuser7\nuser8\nuser9"
    pwd = "pwd1\npwd2\npwd3\npwd4\npwd5\npwd6\npwd7\npwd8\npwd9"
    with open("user.txt",'w') as f: f.write(user)
    with open("pwd.txt",'w') as f: f.write(pwd)
    
    
    ##### main script #####
    
    # load all pwds
    f2 = open("pwd.txt", "r")
    lstpwd = f2.readlines()
    
    f1 = open("user.txt", "r")
    threads = []
    def brute():
        for letter in f1.readlines():
            print ("\n[+]First Value: {}".format(letter.strip()))
            for second_letter in lstpwd:
                print ("[++]Second Value: {}".format(second_letter.strip()))
                threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))
    
        for thread in threads:
            thread.start()
    
        for thread in threads:
            thread.join()
        
    def runner(word1,word2):
        print("[+]I am just a worker class: {0}:{1}".format(word1.strip(),word2.strip()))
        
    brute()
    

    输出

    [+]First Value: user1
    [++]Second Value: pwd1
    [++]Second Value: pwd2
    [++]Second Value: pwd3
    .......    
    [+]First Value: user2
    [++]Second Value: pwd1
    [++]Second Value: pwd2
    [++]Second Value: pwd3
    .......
    [+]I am just a worker class: user1:pwd1
    [+]I am just a worker class: user1:pwd2
    [+]I am just a worker class: user1:pwd3
    .......
    [+]I am just a worker class: user2:pwd1
    [+]I am just a worker class: user2:pwd2
    [+]I am just a worker class: user2:pwd3
    .......
    

    【讨论】:

    • 首先非常感谢伙计!另外,我看到你的代码工作得很好,我还在 python 2.7 中测试了用例,它也工作了,因为我看到你第一次打电话f2.readlines() 一旦你在没有循环的情况下调用这个函数,这会创建一个列表吗?然后在 main_loop 中调用变量时最终获取它们?
    • 是 - lstpwd = f2.readlines() 从文件中的行创建一个列表
    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2020-07-26
    • 1970-01-01
    • 2019-11-29
    • 2011-02-15
    相关资源
    最近更新 更多