需要帮助确定为什么在每行上出现随机字符

时间:2019-03-27 17:02:58

标签: python

import os
import sys
import fileinput
import random
import string

stringLength=10
"""Generate a random string of fixed length """
letters = string.ascii_lowercase
Password = ''.join(random.choice(letters) for i in range(stringLength))

print ("Text to search for:")
textToSearch = input( "> " ) 

print ("File to perform Search-Replace on:")
fileToSearch  = input( "> " )

tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
    if textToSearch in line :
        print('Match Found')
    else:
        print('Match Not Found!!')
    tempFile.write( line.replace( textToSearch, Password ) )
tempFile.close()

input( '\n\n Press Enter to exit...' )

这是发生了什么

start /min MinecraftClient.exe qQ47n2 - mc.cosmicmc.com

start /min MinecraftClient.exe qQ47n2 - mc.cosmicmc.com

start /min MinecraftClient.exe qQ47n2 - mc.cosmicmc.com

qQ47n2正在发生变化 我希望每行都是随机的,但正如您所见,每行向下都有qQ47n2吗?

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的问题来自以下事实:在for循环之外,您仅生成了一次密码。

您可以在tempFile.write( line.replace( textToSearch, Password ) )之前生成密码。