簡體   English   中英

如何允許我的程序的特定部分寫入 txt

[英]How to allow a specific part of my program to write to a txt

好吧,所以我已經看到了多種方法來允許 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,flush=True, end=inbetween)
      odd=odd+2
    elif eocr=="c":
      print(consec,flush=True, end=inbetween)
      consec=consec+1
    elif eocr=="r":
      print(random.randint(int(randlowcap),int(randhighcap)),flush=True, end=inbetween)
    elif eocr=="p":
      password=random.sample(chars,letters)
      divider = ""
      password = divider.join(password) 
      print(password,flush=True, end=inbetween)
    elif eocr=="pr":
      password=choices(chars, k=letters)
      divider = ""
      password = divider.join(password) 
      print(password,flush=True, 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, The final number will be",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, The final number will be",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, It will print",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, It will print",times,"passwords,\nEach with",letters," 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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM