Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Gerador de Links em Python - FeL 486
- import random
- import os.path
- def fclear(file): # limpa um arquivo, file -> nome do arquivo
- f = open(file, 'w')
- f.write("")
- f.close()
- def freplace(file, txt1, txt2): # Substitui, em um arquivo, ocorrência de texto 'txt1' por 'txt2'
- arquivo = open(file, 'r')
- texto = ""
- novotexto = ""
- while True:
- linha = arquivo.readline()
- if not linha:
- break
- else:
- texto += linha
- arquivo.close()
- if(txt1 in texto):
- texto = str.replace(texto, txt1, txt2)
- print(texto)
- arquivo = open(file, 'w')
- arquivo.write(texto)
- arquivo.close()
- def fedit(file, outroTexto): # Acrescenta ao arquivo mais conteúdo, sendo 'outroTexto' o adicional.
- if(os.path.exists(file) == False):
- arquivo = open(file, 'w')
- arquivo.close()
- arquivo = open(file, 'r')
- texto = ""
- while True:
- linha = arquivo.readline()
- if not linha or linha == "":
- break
- else:
- texto += linha
- arquivo.close()
- texto += "\n"
- texto += outroTexto
- arquivo = open(file, 'w')
- arquivo.write(texto)
- arquivo.close()
- def gerarLink(quant, usar_numeros):
- link = ""
- for a in range(0, quant):
- if(a == 0):
- if(random.randint(0, 1) == 0):
- link += chr(random.randint(65, 90)) # Letra maiuscula
- else:
- link += chr(random.randint(97, 122)) # Letra maiuscula
- else:
- value = random.randint(0, 2)
- if(usar_numeros == True):
- if(value == 0):
- link += chr(random.randint(65, 90)) # Letra maiuscula
- elif(value == 1):
- link += chr(random.randint(97, 122)) # Letra maiuscula
- else:
- link += chr(random.randint(48, 57)) # Numeros
- else:
- if(random.randint(0, 1) == 0):
- link += chr(random.randint(65, 90)) # Letra maiuscula
- else:
- link += chr(random.randint(97, 122)) # Letra maiuscula
- return link
- print("Gerador de links em Python - FeL 486\n")
- usar_numericos = ""
- quant = ""
- while quant.isdigit() == False:
- quant = input("Insira quantos caracteres deseja gerar: ")
- while str(usar_numericos) != "Sim" and str(usar_numericos) != "Nao":
- usar_numericos = str(input("Deseja utilizar números no link? Sim ou Não? "))
- quant = int(quant)
- usar_numericos = True if usar_numericos == "Sim" else False
- link = gerarLink(quant, usar_numericos)
- fedit('links.txt', link)
- print("Link gerado: %s" % link)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement