问题描述
好的,所以我已经看到了多种方法来允许 python 程序写入指定的 txt,但是我所看到的没有一种方法与我的程序兼容。
这是一个数字/密码生成器,可以打印从一行到实际数十亿行的字符串。但是,问题是在打印出所选字符串之前有一些对话框和选项。
如何使它只记录某个部分并将其写入 txt,而不是整个内容。
这里是github的链接,附上程序。 https://github.com/JasonDerulo1259/JasonsGenerator
或者,这里是原始代码:
import time
import random
from random import randint
from random import choices
import sys
chars= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","$","%","&","(",")","*",","-",".","/",":",";","<","=",">","?","@","[","]","^","_","`","{","|","}","~","0","1","2","3","4","5","6","7","8","9"]
def massprint():
againagain = int(times)
even=0
odd=1
consec=0
rand=0
while againagain >= 0:
time.sleep(float(speed))
againagain -= 1
if eocr=="e":
print(even,flush=True,end=inbetween)
even=even+2
elif eocr=="o":
print(odd,end=inbetween)
odd=odd+2
elif eocr=="c":
print(consec,end=inbetween)
consec=consec+1
elif eocr=="r":
print(random.randint(int(randlowcap),int(randhighcap)),end=inbetween)
elif eocr=="p":
password=random.sample(chars,letters)
divider = ""
password = divider.join(password)
print(password,end=inbetween)
elif eocr=="pr":
password=choices(chars,k=letters)
divider = ""
password = divider.join(password)
print(password,end=inbetween)
else:
print("Unrecognized. Type either e,o or c or r")
if againagain<=1:
print(" \n")
print("even,odd,consecutive,random,password or password-repeat ")
eocr=input("e/o/c/r/p/pr ")
time.sleep(0.5)
times=input("How many times/strings? ")
againagain=int(times)
time.sleep(0.5)
speed=input("What speed should it print,\nAnswer in seconds. (0.02 Is normal-ish speed) ")
time.sleep(0.5)
inbetween=input("What should be inbetween each string (eg: space,comma,newline (/n).). \nAnswer with the string inbetween,Not the name of it \n(write ' ',not 'space') ")
if inbetween=="/n":
inbetween="\n"
else:
print("")
if eocr=="e":
print("Alright,The final number will be",str(againagain * 2))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
print("\n")
elif eocr=="o":
print("Alright,str(againagain * 2+1))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
print("\n")
elif eocr=="c":
print("Alright,str(againagain))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
elif eocr=="r":
time.sleep(0.5)
randhighcap=input("And what do you want the highest random number to be? ")
time.sleep(0.5)
randlowcap=input("And what do you want the lowest random number to be? ")
time.sleep(0.5)
print("Alright,It will print",str(againagain),"random numbers \nWith a high cap of",randhighcap,"\nAnd a low cap of",randlowcap)
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
print("\n")
elif eocr=="p":
letters=int(input("Alright,How many characters should the password have? (no higher than 88) "))
time.sleep(0.5)
print("Okay,times,"passwords,\nEach with",letters," NON-REPEATING characters each")
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
times=int(times)-1
times=str(times)
massprint()
time.sleep(1)
print("Done!")
elif eocr=="pr":
letters=int(input("Alright,How many characters should the password have? "))
time.sleep(0.5)
print("Okay," REPEATING characters each")
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
times=int(times)-1
times=str(times)
massprint()
time.sleep(1)
print("Done!")
else:
print("Restart and input 'e' or 'o' or 'c' or 'r' or 'p'")
print("Press the Enter key to exit")
exit1=input(" ")
exit()
解决方法
将密码分配给一个列表,其中每个字符是列表中的一个单元格。这可以用来简单地写入 txt 文件。以下代码执行此操作:
file = open("TEST.txt","a")
password = ["1","2","3","4"]
a = str(password)
file.write(a)
#optional - But outputs the full contents of the file
file = open("TEST.txt","r")
print(file.read())