Advertisement
fel486

Gerador de links em Python - File save!

Apr 17th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # Gerador de Links em Python - FeL 486
  2.  
  3. import random
  4. import os.path
  5.  
  6. def fclear(file): # limpa um arquivo, file -> nome do arquivo
  7.     f = open(file, 'w')
  8.     f.write("")
  9.     f.close()
  10.  
  11. def freplace(file, txt1, txt2): # Substitui, em um arquivo, ocorrência de texto 'txt1' por 'txt2'
  12.     arquivo = open(file, 'r')
  13.     texto = ""
  14.     novotexto = ""
  15.    
  16.     while True:
  17.         linha = arquivo.readline()
  18.         if not linha:
  19.             break
  20.         else:
  21.             texto += linha
  22.  
  23.     arquivo.close()    
  24.  
  25.     if(txt1 in texto):
  26.         texto = str.replace(texto, txt1, txt2)
  27.  
  28.     print(texto)
  29.    
  30.     arquivo = open(file, 'w')
  31.     arquivo.write(texto)
  32.     arquivo.close()
  33.    
  34.  
  35. def fedit(file, outroTexto): # Acrescenta ao arquivo mais conteúdo, sendo 'outroTexto' o adicional.
  36.     if(os.path.exists(file) == False):
  37.         arquivo = open(file, 'w')
  38.         arquivo.close()        
  39.        
  40.     arquivo = open(file, 'r')
  41.     texto = ""
  42.    
  43.     while True:
  44.         linha = arquivo.readline()
  45.         if not linha or linha == "":
  46.             break
  47.         else:
  48.             texto += linha
  49.            
  50.     arquivo.close()
  51.     texto += "\n"
  52.     texto += outroTexto
  53.  
  54.     arquivo = open(file, 'w')
  55.     arquivo.write(texto)
  56.     arquivo.close()  
  57.  
  58. def gerarLink(quant, usar_numeros):
  59.     link = ""
  60.     for a in range(0, quant):
  61.         if(a == 0):
  62.             if(random.randint(0, 1) == 0):
  63.                 link += chr(random.randint(65, 90)) # Letra maiuscula
  64.             else:
  65.                 link += chr(random.randint(97, 122)) # Letra maiuscula
  66.         else:
  67.             value = random.randint(0, 2)
  68.             if(usar_numeros == True):
  69.                 if(value == 0):
  70.                     link += chr(random.randint(65, 90)) # Letra maiuscula
  71.                 elif(value == 1):
  72.                     link += chr(random.randint(97, 122)) # Letra maiuscula
  73.                 else:
  74.                     link += chr(random.randint(48, 57)) # Numeros
  75.             else:
  76.                 if(random.randint(0, 1) == 0):
  77.                     link += chr(random.randint(65, 90)) # Letra maiuscula
  78.                 else:
  79.                     link += chr(random.randint(97, 122)) # Letra maiuscula
  80.                
  81.     return link
  82.      
  83. print("Gerador de links em Python - FeL 486\n")
  84.  
  85. usar_numericos = ""
  86. quant = ""
  87.  
  88. while quant.isdigit() == False:
  89.     quant = input("Insira quantos caracteres deseja gerar: ")
  90.    
  91. while str(usar_numericos) != "Sim" and str(usar_numericos) != "Nao":
  92.     usar_numericos = str(input("Deseja utilizar números no link? Sim ou Não? "))
  93.  
  94. quant = int(quant)
  95. usar_numericos = True if usar_numericos == "Sim" else False
  96.  
  97. link = gerarLink(quant, usar_numericos)
  98.    
  99. fedit('links.txt', link)
  100.  
  101. print("Link gerado: %s" % link)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement