【发布时间】:2021-05-15 13:22:01
【问题描述】:
我给你一个例子我点击运行在控制台中写一个数字(多少个键)然后我得到这个随机字母和数字:61PYY-ZEPY1-2H82R-V1JZ1-9VEF7,但我希望它们在我的 demofile2.txt。当我将其粘贴到写入中时:'-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)) 它说 seq 未定义
这是代码:
import random, sys
class KeyGen():
def __init__(self):
global i
i = int(input("How many serial codes are you looking for? \n"))
print("")
self.main(i)
def main(self, count):
""" Our main iteration function, using simple
capital letters, along with the numbers 0-9 """
seq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
for i in range(count):
print('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)))
print("\nCreated {} serial keys.".format(count))
if __name__ == '__main__':
app = KeyGen()
text_file = open("demofile2.txt.")
text_file.write(THISISWHATISEARCH)
text_file.close()
【问题讨论】:
-
这能回答你的问题吗? Print string to text file
-
我知道如何打开文件并写入文件,但不知道密钥代码是什么
-
您的问题是:“我不知道如何通过 python 写入 .txt 文件”,现在您说您知道。你的问题对我来说不是很清楚。我没有看到任何尝试打开文件或在代码中写入文件
-
text_file = open("demofile2.txt.") text_file.write(这部分是我在这里搜索的应该是key) text_file.close()
-
将
print替换为text_file.write....?