Python写入文件
我想将随机生成的散列写入文本文件,但文本文件中的每一次都不是文本。我能做什么? 这是我的密码:Python写入文件,python,file,hash,Python,File,Hash,我想将随机生成的散列写入文本文件,但文本文件中的每一次都不是文本。我能做什么? 这是我的密码: import hashlib import string from random import randint import random import time rnd = randint(10,20) while True: rndChar = "" for _ in range(rnd): rndChar += random.choice(string.as
import hashlib
import string
from random import randint
import random
import time
rnd = randint(10,20)
while True:
rndChar = ""
for _ in range(rnd):
rndChar += random.choice(string.ascii_letters + string.digits)
md5out = hashlib.md5(rndChar).hexdigest()
float.fromhex('0.' + md5out[:16])
float.fromhex('0.' + md5out[:16])
print md5out
file = open('/home/testuser/Desktop/hash.txt', 'w')
file.write(str(md5out))
file.close()
控制台输出如下所示:
2720df62db5dd1881b011198280eb993
283eafdd484385b4396c34df58936c85
91aeb7daf22c53531b3f56b6d640a948
784c7aaf8fe24148f4ffef7376951385
49557f13c14a3022dbaae67f39d5af5e
881a4744d9bbd996b2408fccff40f521
21ff71063f55c91051b609ad40d31d7c
所以它可以工作,但不会显示在我桌面上的文本文件中。请帮助将其打开为
file=Open('/home/testuser/Desktop/hash.txt','a')
,因为每次“w”都会清除文本文件
这是正确的密码
导入hashlib
导入字符串
从随机导入randint
随机输入
导入时间
rnd = randint(10,20)
while True:
rndChar = ""
for _ in range(rnd):
rndChar += random.choice(string.ascii_letters + string.digits)
md5out = hashlib.md5(rndChar).hexdigest()
float.fromhex('0.' + md5out[:16])
float.fromhex('0.' + md5out[:16])
print md5out
file = open('/home/testuser/Desktop/hash.txt', 'a')
file.write(str(md5out))
file.close()
将文件的打开和关闭放在while循环之外。每次你打开它,它都会被清除。谢谢,我怎样才能将每个哈希写在一行新的内容中?没问题-如果你发现它有效/有帮助,请将其设置为答案/向上投票,因为它确实有帮助-恐怕不知道如何在多个lne上写评论